假设我定义了以下协议:
// 用户界面对象的基本协议:
@protocol UIObjectProtocol <NSObject>
@property (assign) BOOL touchable;
@end
// 一个用户界面对象的基本协议,该对象由一个holder
对象持有:
@protocol UIHeldObjectProtocol <UIObjectProtocol>
@property (readonly) id holder;
@end
以下是类继承结构:
// User Interface对象的基类,包含touchable
属性
@interface UIObject : NSObject <UIObjectProtocol> {
BOOL _touchable;
}
@end
@implementation UIObject
@synthesize touchable=_touchable;
@end
此时,一切都好。然后我创建了一个名为 UIPlayingCard
的 UIObject
子类。由于其父类也符合 UIObjectProtocol
,因此 UIPlayingCard
本身也符合。
现在假设我想让 UIPlayingCard
符合 UIHeldObjectProtocol
,所以我进行了以下操作:
@interface UIPlayingCard : UIObject <UIHeldObjectProtocol> {
}
@end
@implementation UIPlayingCard
-(id)holder { return Nil; }
@end
请注意,UIPlayingCard
符合 UIHeldObjectProtocol
,而 UIHeldObjectProtocol
间接符合 UIObjectProtocol
。然而,在 UIPlayingCard
中我得到了编译器警告,如下所示:这意味着警告:属性“touchable”需要定义方法“-touchable” - 使用 @synthesize、@dynamic 或提供方法实现
UIPlayingCard
超类对 UIObjectProtocol
的符合没有被继承(可能是因为在 UIObject
实现范围内声明了 @synthesize
指令)。我是否必须在
UIPlayingCard
实现中重新声明 @synthesize
指令?@implementation UIPlayingCard
@synthesize touchable=_touchable; // _touchable now must be a protected attribute
-(id)holder { return Nil; }
@end
是否有其他方法可以消除编译器警告?这是不良设计的结果吗?
提前感谢您的回答。