开发者

Why isn't my objective-C child class inheriting the property of it's parent?

In the .h and .m files of my parent class (WebServiceMessage) I have the following lines:

@pr开发者_如何学JAVAoperty(nonatomic, retain) NSMutableData *receivedData;
@synthesize receivedData;

My child class has the following definition:

@interface RegistrationMessage : WebServiceMessage{...}

-(bar)foo;

So in theory, the child class should be able to use receivedData without needing to declare it itself -- it should inherit the existence of that variable from it's parent.

@implementation RegistrationMessage
...
-(bar)foo{
    self.receivedData=[NSMutableData data];//Error:  Property 'receivedData' not found on object of type 'Class'
}

I've checked, and I'm using #import for the parent class in both the .h and the .m files, just to be sure, but the error is still showing up.


Based on the error, it looks like you're omitting an important detail: foo is not an instance method, it's a class method (that is, it's + (void)foo rather than - (void)foo). You can only set properties (or instance variables in general) on instances.


Something is missing the the code that you duid not show, the below compiles fine:

@interface WebServiceMessage : NSObject
@property(nonatomic, retain) NSMutableData *receivedData;
@end

@implementation WebServiceMessage
@synthesize receivedData;
@end

@interface RegistrationMessage : WebServiceMessage
-(void)foo;
@end

@implementation RegistrationMessage
-(void)foo {
    self.receivedData = [NSMutableData data];
}
@end

I did have to get rid of the bar return type because it is an unknown type and there was no return statement in method foo.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜