accessing the parent of an object
I'm trying to call methods on the parent of my object by passing the parent in as property. But i keep getting this error:
expected specifier-qualifier-list before 'Wh开发者_开发技巧eel'
@interface Car : NSObject {
Wheel *w;
}
- (void)doCarStuff;
@end
@implementation Car
- (id)init {
if((self = [super init])) {
//w = [[Wheel alloc] init];
//w.parent = self;
}
return self;
}
- (void)doCarStuff {
NSLog(@"Car stuff");
}
@end
@interface Wheel : NSObject {
Car *parent;
}
@property (nonatomic, assign) Car *parent;
@end
@implementation Wheel
@synthesize parent;
- (id)init {
if((self = [super init])) {
[parent doCarStuff];
}
return self;
}
@end
It is probably because i have to declare the Car before the Wheel and vise versa. I bet the solution is so simple i can't see it :P
Forward-declare Wheel before Car.
@class Wheel;
@interface Car : ...
(BTW, in Wheel's -init
method, parent
is not initialized (thus always nil
), therefore calling [parent doCarStuff]
there is useless.)
精彩评论