【发布时间】:2011-07-07 08:26:00
【问题描述】:
假设我定义了以下协议:
// 用户界面对象的基本协议:
@protocol UIObjectProtocol <NSObject>
@property (assign) BOOL touchable;
@end
//holder 对象持有的用户界面对象的基本协议:
@protocol UIHeldObjectProtocol <UIObjectProtocol>
@property (readonly) id holder;
@end
以及以下类层次结构:
// 用户界面对象的基类,它综合了touchable 属性
@interface UIObject : NSObject <UIObjectProtocol> {
BOOL _touchable;
}
@end
@implementation UIObject
@synthesize touchable=_touchable;
@end
此时,一切正常。然后我创建了一个名为UIPlayingCard 的UIObject 子类。本质上,UIPlayingCard 符合 UIObjectProtocol,因为它的超类也这样做。
现在假设我希望 UIPlayingCard 符合 UIHeldObjectProtocol,所以我执行以下操作:
@interface UIPlayingCard : UIObject <UIHeldObjectProtocol> {
}
@end
@implementation UIPlayingCard
-(id)holder { return Nil; }
@end
请注意,UIPlayingCard 符合 UIHeldObjectProtocol,后者可传递地符合 UIObjectProtocol。但是我在UIPlayingCard 中收到编译器警告,例如:
警告:属性 'touchable' 需要 要定义的方法“-touchable” - 使用@synthesize、@dynamic 或提供 方法实现
这意味着 UIPlayingCard 超类与 UIObjectProtocol 的一致性没有被继承(可能是因为 @synthesize 指令是在 UIObject 实现范围中声明的)。
我是否有义务在 UIPlayingCard 实现中重新声明 @synthesize 指令?
@implementation UIPlayingCard
@synthesize touchable=_touchable; // _touchable now must be a protected attribute
-(id)holder { return Nil; }
@end
或者还有其他方法可以消除编译器警告?会不会是设计不好的结果?
提前致谢,
【问题讨论】:
-
这与您的问题无关,但在您自己的类/协议/常量/whatevers 中使用“Apple 的”前缀通常不是最好的主意:您永远不知道会包含什么大水果下一个版本...
-
我同意你的观点 danyowdee,事实上我并没有在我的应用程序中使用“UI”前缀,我确实遵循了你列出的所有这些良好做法。我只是在帖子中隐藏了真正的前缀(例如公司前缀),因为它与问题无关。
标签: iphone objective-c properties protocols compiler-warnings