0

NSCollectionViewに準拠するデリゲートがありNSCollectionViewDelegateます。

- (id < NSCollectionViewDelegate >)delegate

を拡張する新しいプロトコルがありますNSCollectionViewDelegate

@protocol extendedProtocol <NSCollectionViewDelegate>

ここで、CollectionViewItem のコントローラー クラスで、次の方法でデリゲートのメソッドを呼び出そうとします。

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

「インスタンス メソッド 'doSomeWork:' が見つかりません」という警告が表示され続けます。

やってみた

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

しかし、「型 'id' の式で 'id' を初期化する互換性のないポインター型です。

NSCollectionViewデリゲートのプロトコルを変更する正しい方法は何ですか?

4

1 に答える 1

1

キャストが必要です。次のようにします。

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

または、2番目の例では:

id <extendedProtocol> dg = (id <extendedProtocol>) [[self collectionView] delegate];
于 2012-02-01T13:42:47.383 に答える