質問

クラス用に作成しているカテゴリが、プロトコルによって設定された契約を満たすメソッドを追加する場合、そのカテゴリクラスにプロトコルの実装としてフラグを立て、それによってOBJ-C Preprocessorに示すことを望みます。クラスは、プロトコルも効果的に実装しています。

例の例(明確にしてください、オレに感謝します!):

@protocol SomeDelegate <NSObject>
  - (void)someDelegateMessage;
@end

例カテゴリ:

@interface NSObject (SomeCategory) <SomeDelegate>
  - (void)someDelegateMessage;    
@end

そして、それ以外の場合は典型的な実装があります

@implement NSObject (SomeCategory)
  - (void)someDelegateMessage {}
@end

実際にこれを試してみると、各nsobjectメソッドについて警告が表示されます。

警告:カテゴリ「Somecategory」の不完全な実装

警告:「 - 説明」のメソッド定義が見つかりません

...

警告: '-isequal:'のメソッド定義は見つかりません

警告:カテゴリ「Somecategory」は「nsobject」プロトコルを完全に実装していません

削除すると正常に動作します <SomeDelegate> 宣言からですが、もちろんnsobjectはSomedelegateとして認識されていません

役に立ちましたか?

解決

回避策は、実装のないカテゴリでプロトコルを宣言し、別のカテゴリにメソッドを実装することです。

@interface NSObject (SomeCategory) <SomeDelegate>
  - (void)someDelegateMessage;    
@end

@implementation NSObject (SomeCategory_Impl)
  - (void)someDelegateMessage {}
@end

これを行うと、 NSObject 適合すると見なされます <SomeDelegate> コンパイル時に、およびランタイムチェックの場合 someDelegateMessage 成功します。でも、 conformsToProtocol: ランタイムチェックは失敗します。

もちろん、そうすべきです バグを提出します コアクラスで宣言されたメソッドを要求すると、警告が生成されません。

他のヒント

Any chance your protocol declaration includes the NSObject protocol? Like this:

@protocol SomeDelegate <NSObject>
...

That's where the warnings are coming from because now your category does not implement the full protocol. In the test code I just typed up, removing NSObject from the protocol removes the compiler warnings.

If you want the compiler to shut up about sending <NSObject> messages (and its important that you remember that thats the protocol name, not the class name) then just use 'id' variables, not 'id' since thats you explicitly telling the compiler "This is an object which only implements the SomeDelegate protocol".

Alternately, use NSObject as your variable type instead.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top