访问对象的父级

时间:2022-12-26 19:16:29

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 'Wheel'

'Wheel'之前的预期说明符 - 限定符列表

@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

这可能是因为我必须在车轮之前宣布汽车,反之亦然。我打赌解决方案很简单,我看不到它:P

1 个解决方案

#1


2  

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.)

(顺便说一句,在Wheel的-init方法中,parent未初始化(因此总是为nil),因此调用[parent doCarStuff]没有用。)

#1


2  

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.)

(顺便说一句,在Wheel的-init方法中,parent未初始化(因此总是为nil),因此调用[parent doCarStuff]没有用。)