Hi Rob,

I think we could actually do that (and cause the same bug) with the derived 
classes Anton mentioned, which means the behaviour is inconsistent, despite the 
perhaps safety.

- Rod

> On 11 Mar 2017, at 6:32 am, Rob Mayoff <[email protected]> wrote:
> 
>> On Fri, Mar 10, 2017 at 6:08 AM, Rod Brown via swift-evolution 
>> <[email protected]> wrote:
>> Hi everyone. I found something odd that seems baked into how Cocoa Touch 
>> does protocols, but Swift cannot model it:
>> 
>> 
>> @interface UIScrollView: UIView
>> 
>> @property (weak, nonatomic) id <UIScrollViewDelegate> delegate;
>> 
>> @end
>> 
>> @interface UITableView: UIScrollView
>> 
>> @property (weak, nonatomic) id <UITableViewDelegate> delegate;
>> 
>> @end
>> 
>> @protocol UITableViewDelegate: UIScrollViewDelegate
>> ...
>> @end
> 
> The problem here is that `UITableView`'s redefinition of `delegate` is not 
> type-safe. By casting a `UITableView` reference to a `UIScrollView`, you set 
> the `delegate` to something that doesn't conform to `UITableView`:
> 
>     @interface ViewController () <UIScrollViewDelegate>
>     @end
> 
>     @implementation ViewController
> 
>     - (void)viewDidLoad {
>         [super viewDidLoad];
> 
>         UITableView *tableView = [[UITableView alloc] init];
> 
>         // This line correctly generates a warning, because self isn't a 
> UITableViewDelegate:
>         tableView.delegate = self;
> 
>         // This line generates no warning:
>         ((UIScrollView *)tableView).delegate = self;
>     }
> 
>     @end
> 
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to