Re: How to remove 'not found in protocol' compiler warning
Re: How to remove 'not found in protocol' compiler warning
- Subject: Re: How to remove 'not found in protocol' compiler warning
- From: Thomas Davie <email@hidden>
- Date: Fri, 12 Nov 2010 14:35:56 +0000
On 12 Nov 2010, at 14:17, Sherm Pendley wrote:
> On Fri, Nov 12, 2010 at 9:09 AM, Paul Johnson <email@hidden> wrote:
>
>> I'm getting a compiler warning message at the following line of code:
>>
>> NSArray *selectedObjects = [[secondTableView dataSource]
>> selectedObjects];
>>
>> The warning message is <'selectedObjects' not found in protocol>.
>>
>
> Note that NSTableView's -dataSource returns an object of type
> id<NSTableViewDataSource>. So, the compiler knows nothing about the object
> other than its protocol conformance. Since -selectedObjects isn't part of
> the protocol, it gives this warning.
>
>
>> Can someone suggest what I need to do to remove the warning?
>>
>
> Use a type cast to tell the compiler what class the data source really is:
>
> NSArray *selectedObjects = [(MyDataSourceClass*)[secondTableView
> dataSource] selectedObjects];
While this will work, I don't agree that it's the "correct" solution to the problem. The issue here is that you are making an assumption about the TV's data source that may not be true. Instead, what should be happening is that if you want to send messages to that particular object with the knowledge that it's the right type to receive those messages, you should maintain your own pointer to the object, and you should send messages there instead.
This is roughly the same reason as why you shouldn't be sending messages to [NSApp delegate] – that's the app delegate, and the app is responsible for sending it messages, not you... if you want to send messages to a controller that just so happens to be the same object, maintain your own pointer to it with a more specific type.
Bob_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden