Objective-C,协议和子类

Edu*_*lho 10 iphone protocols properties objective-c compiler-warnings

假设我定义了以下协议:

//用户界面对象的基本协议:

@protocol UIObjectProtocol <NSObject>
@property (assign) BOOL touchable;
@end
Run Code Online (Sandbox Code Playgroud)

//对象持有的用户界面对象的基本协议holder:

@protocol UIHeldObjectProtocol <UIObjectProtocol>
@property (readonly) id holder;
@end
Run Code Online (Sandbox Code Playgroud)

以下类层次结构:

//用户界面对象的基类,用于合成touchable属性

@interface UIObject : NSObject <UIObjectProtocol> {
   BOOL _touchable;
}
@end

@implementation UIObject
@synthesize touchable=_touchable;
@end
Run Code Online (Sandbox Code Playgroud)

在这一点上,一切都很好.然后我创建一个UIObject名为的子类UIPlayingCard.本质上,UIPlayingCard符合UIObjectProtocol它,因为它的超类也是如此.

现在假设我想要UIPlayingCard遵守UIHeldObjectProtocol,所以我做了以下事情:

@interface UIPlayingCard : UIObject <UIHeldObjectProtocol> {
}
@end

@implementation UIPlayingCard
-(id)holder { return Nil; }
@end
Run Code Online (Sandbox Code Playgroud)

请注意,UIPlayingCard符合UIHeldObjectProtocol,传递符合UIObjectProtocol.但是我得到了编译器警告UIPlayingCard:

警告:属性'touchable'需要定义方法'-touchable' - 使用@synthesize,@ dynamic或提供方法实现

这意味着UIPlayingCard超类的一致性UIObjectProtocol没有被继承(可能是因为该@synthesize指令是在UIObject实现范围内声明的).

我是否有义务@synthesizeUIPlayingCard实施中重新声明该指令?

@implementation UIPlayingCard
@synthesize touchable=_touchable; // _touchable now must be a protected attribute
-(id)holder { return Nil; }
@end
Run Code Online (Sandbox Code Playgroud)

或者还有另一种摆脱编译器警告的方法?这会是糟糕设计的结果吗?

提前致谢,

Ano*_*mie 2

我将用另一种方式来消除警告: use @dynamic,并且编译器将假定将以其他方式提供实现,而不是在类实现中的声明中提供(在本例中,它由超类提供) 。