I have a super class ‘Node’ and several sub classes e.g. ‘SubNode1’, ‘SubNode2’ etc.
I need to have a property which all the sub classes need called ‘parentNode’. It gets set when init is called on ‘SubNode1’ and the object is passed through of type ‘Node’. The reason I used a sub class is because these objects are comforming to a protocol and I cannot use the sub class each time – I need to pass ‘Node’ around.
What I don’t know is how to structure my objects. Where should the release be and where should the property be and should it be retain as I constantly get exe_bad_access errors because of this.
This is how I have structured it currently:
Node.h
@property (nonatomic, retain) Node *parentNode;
Node.m
- (void)dealloc {
[parentNode release];
[super dealloc];
}
SubNode1.h
@interface SubNode1 : Node
{
// No reference to parentNode property
}
SubNode1.m
- (id)initWithParentNode:(SubNode1 *)theParentNode {
self = [super init];
if (self)
{
super.parentNode = theParentNode;
}
return self;
}
- (void)dealloc {
[super dealloc];
}
You’re already designing it the correct way. The error is probably that:
should be: