سؤال

NSCollectionView has a delegate which should conform to NSCollectionViewDelegate.

- (id < NSCollectionViewDelegate >)delegate

I have a new protocol, which extends NSCollectionViewDelegate.

@protocol extendedProtocol <NSCollectionViewDelegate>

Now, in my CollectionViewItem's controller class, I try to call the delegate's method in this way:

if (
    [self collectionView] 
    && [[self collectionView] delegate] && 
    [[[self collectionView] delegate] conformsToProtocol:@protocol(extendedProtocol)]
        ) 
{
BOOL flag = [[[self collectionView] delegate] doSomeWork:@"abc"];
}

I keep getting warning that "Instance Method 'doSomeWork:' not found".

I tried doing

id <extendedProtocol> dg = [[self collectionView] delegate];
BOOL flag = [dg doSomeWork:@"abc"];

But then I get warning, "Incompatible pointer types initializing 'id' with an expression of type 'id'.

What is the correct way of changing the protocol of NSCollectionView delegate?

هل كانت مفيدة؟

المحلول

You need a cast. Either like this:

BOOL flag = [(id <extendedProtocol>) [[self collectionView] delegate] doSomeWork:@"abc"];

Or in your second example:

id <extendedProtocol> dg = (id <extendedProtocol>) [[self collectionView] delegate];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top