How can I change or extend the NSCollectionView's delegate?
-
21-04-2021 - |
Question
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?
Solution
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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow