I’m very new to objective-c and programming. I had read in one of the questions posted on stackoverflow regarding objective-c that it was “bad form” to directly access the instance variable(s) of a super class. Below is an example of what I thought it meant to directly access an instance variable from a super class (Example 1), and what I think it might mean to use correct form (Example 2):
// ExampleSuperClassMonster.h
@interface ExampleSuperClassMonster : CCSprite {
int hp; // hitpoints
}
@property (readwrite) int hp;
@end
// ExampleSubClassMonster.h
@interface ExampleSubClassMonster : ExampleSuperClassMonster {
@end
// Example 1: is this bad form??
ExampleSubClassMonster *subClassMonster = [[ExampleSubClassMonster alloc] init];
subClassMonster.hp = 100;
// Example 2: is this correct form??
ExampleSubClassMonster *dummyMonster = [[super alloc] init];
dummyMonster.hp = 100;
ExampleSubClassMonster *subClassMonster = [[ExampleSubClassMonster alloc] init];
subClassMonster.hp = dummyMonster.hp;
[dummyMonster release];
Apologies if this question is irrelevant or I’m not making sense. Also, apologies if I’ve misunderstood what it might mean to have bad form in accessing instance variables of a super class. Also, apologies if this questions has already been asked (I couldn’t find any similar questions) — if it has, a link to that question would be helpful. Thanks.
Example 1 is fine.
[super init]should be called from theinitmethod of the sub class.