(Not So) Silly Objective-C inheritance problem when using property - GCC Bug?

Posted by Ben Packard on Stack Overflow See other posts from Stack Overflow or by Ben Packard
Published on 2010-05-06T04:13:30Z Indexed on 2010/05/06 6:48 UTC
Read the original article Hit count: 296

Filed under:
|
|

Update 2 - Many people are insisting I need to declare an iVar for the property. Some are saying not so, as I am using Modern Runtime (64 bit). I can confirm that I have been successfully using @property without iVars for months now. Therefore, I think the 'correct' answer is an explanation as to why on 64bit I suddenly have to explicitly declare the iVar when (and only when) i'm going to access it from a child class. The only one I've seen so far is a possible GCC bug (thanks Yuji). Not so simple after all...

Update - I messed up one line of the original copy and paste - corrected. The @property call was missing (nonatomic, retain) but is a red herring - STILL NEED AN ANSWER! Thanks.

I've been scratching my head with this for a couple of hours - I haven't used inheritance much.

Here I have set up a simple Test B class that inherits from Test A, where an ivar is declared. But I get the compilation error that the variable is undeclared. This only happens when I add the property and synthesize declarations - works fine without them.

TestA Header:

#import <Cocoa/Cocoa.h>
@interface TestA : NSObject {
    NSString *testString;
}
@end

TestA Implementation is empty:

#import "TestA.h"
@implementation TestA  
@end

TestB Header:

#import <Cocoa/Cocoa.h>
#import "TestA.h"
@interface TestB : TestA {
}
@property (nonatomic, retain) NSString *testProp;
@end

TestB Implementation (Error - 'testString' is undeclared)

#import "TestB.h"
@implementation TestB
@synthesize testProp;
- (void)testing{
    NSLog(@"test ivar is %@", testString);
}
@end

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about inheritance