我的 Cocoa AppDelegate 包含类型 ID 对其主视图的引用。该引用是多态的,因为它可能指向 PDFView 的子类或 NSImageView 的子类,具体取决于视图图像的来源。两个视图子类都实现了相同的协议,所以我的 AppDelegate 不必知道它正在处理什么类型的视图。但是,每次我调用其中一个协议方法时,我都会收到一条警告,上面写着“找不到实例方法'-methodName'(返回类型默认为'id')”。我可以忽略警告或通过使用“performSelector:(@selector(methodName:)”调用协议方法来强制问题。
有没有什么我可以做(或应该做)来消除警告而不诉诸performSelector?
//FLAppDelegate.h
@interface FLAppDelegate : NSObject <NSApplicationDelegate>
{
...
IBOutlet id _formImageView; //type is FLPDFView* or FLImageView*
…
}
//FLFormImageProtocol.h
@protocol FLFormImageProtocol <NSObject>
@required
- (void) methodName;
@end
//FLPDFView.h
@interface FLPDFView : PDFView <FLFormImageProtocol>
@end
//FLImageView.h
@interface FLImageView : NSImageView <FLFormImageProtocol>
@end