我有以下方法:反思參數:id來決定它是否是一個類或協議
-(void)SomeMethod:(id)classOrProtocol;
它將被稱爲像這樣:
[self someMethod:@protocol(SomeProtocol)];
或者
[self someMethod:[SomeClass class]];
在方法體內我需要確定是否| classOrProtocol |是:
任何類(類)或任何協議(協議)或任何其他
[[classOrProtocol class] isKindOfClass: [Protocol class]]
導致(建立)錯誤:
Receiver 'Protocol' is a forward class and corresponding @interface may not exist
所以,我怎麼能知道從一個協議一個類別從其他任何?
感謝您的全面回答。我在與依賴注入相關的代碼中使用這種方法,在這種情況下,我認爲這是有道理的。但也許我應該把它分成兩種方法。 – Undistraction
在內部添加了更多信息。閱讀源代碼很有趣,謝謝你的好問題,1ndivisible! – Yuji