I've recently realized that it is not proper form to access the properties of a class from inside the init using self.whatever = thing, and that you should instead access the properties directly using _whatever = thing. This works fine when I'm setting values for a base class. However, when I try to set values inside the init of a subclass, I get the error "Use of undeclared identifier _whatever". Using self.whatever from inside the init of the subclass works fine.
Just for clarity, for @property int whatever declared in the interface of a base object, this compiles:
-(id) init {
self = [super init];
if (self) {
[self createName];
self.whatever = 100;
}
return self;
}
And this doesn't:
-(id) init {
self = [super init];
if (self) {
[self createName];
_whatever = 100;
}
return self;
}
I think I am misunderstanding something here. I tried searching for what I'm doing wrong, but I don't know the right words to search for.
As requested, the header for the base class:
#import <Foundation/Foundation.h>
@interface DTCharacter : NSObject
@property BOOL isIdle;
@end
I'm using auto synthesize so there is nothing about this variable in the implementation of the base class.
Also the header file of the subclass does not mention or reference the variable.
I'm trying to assign it here in the implementation of the subclass:
-(id) init {
self = [super init];
if (self) {
[self createName];
_isIdle = YES; //says use of undeclared identifier
//this would work: self.isIdle = YES;
}
return self;
}