类是否继承了objective-c中的协议?

时间:2011-05-19 21:04:44

标签: ios4 inheritance protocols ipad

我有一个实现协议的类。在MyProtocol中,我有两个属性:

@protocol MyProtocol <NSObject>
    @property(nonatomic, retain) NSObject *stuff;
    @property(nonatomic, retain) NSString *stringStuff;
@end

我的类实现了如图所示的协议

标题.h

@interface MyClass : UIViewController <MyProtocol>
...

Implementation.m

@implementation MyClass
    @synthesize stuff;
    @synthesize stringStuff;
...

我有一个继承自MyClass ...

的类
@interface MySubClass : MyClass

当我尝试在MySubClass中访问stuff和stringStuff时,我收到一条错误消息,指出MySubClass没有名为'stringStuff'的成员。

如何让子类能够访问父类的这些成员?

2 个答案:

答案 0 :(得分:0)

如果你试图直接访问ivars,你需要在子类中使用属​​性访问吗?

编辑:刚刚意识到我没有回答你原来的问题。是的,如果超类实现了协议,那么子类也将如此。

答案 1 :(得分:0)

不要尝试直接从超类访问ivars,而是使用访问器方法:例如。 self.stuff和self.stringStuff而不是stuff和stringStuff。希望这会有所帮助。

-R