开发者

Do classes inherit protocols in objective-c?

I have a class that implements a protocol . 开发者_开发知识库In MyProtocol, I have two properties:

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

My class implements the protocol as shown

Header .h

@interface MyClass : UIViewController <MyProtocol>
...

Implementation.m

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

I have a class that inherits from MyClass...

@interface MySubClass : MyClass

When I try to access stuff and stringStuff in MySubClass, I get an error saying that MySubClass does not have a member named 'stringStuff.'

How can I get the child classes to be able to access these members of the parent class?


You will need to use property access in the subclass, were you trying to access the ivars directly?

Edit: Just realised I didn't answer your original question. Yes, if a superclass implements a protocol then the subclass will too.


Instead of attempting to access the ivars from the superclass directly, use the accessor methods: eg. self.stuff and self.stringStuff instead of stuff and stringStuff. Hope this helps.

-R

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜