【问题标题】:accessing the parent of an object访问对象的父对象
【发布时间】:2010-07-23 13:00:38
【问题描述】:

我正在尝试通过将父对象作为属性传入来调用对象父对象的方法。 但我不断收到此错误:

“轮子”之前的预期说明符限定符列表

@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

这可能是因为我必须在 Wheel 之前声明 Car,反之亦然。 我敢打赌解决方案很简单,我看不到它:P

【问题讨论】:

    标签: iphone objective-c cocoa-touch oop


    【解决方案1】:

    在 Car 之前向前声明 Wheel。

    @class Wheel;
    
    @interface Car : ...
    

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

    【讨论】:

    • 现在我得到了这个错误,这更加令人困惑。 ld:重复符号 .objc_class_name_Wheel in ..collect2:ld 返回 1 个退出状态命令 /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 失败,退出代码为 1
    猜你喜欢
    • 2017-05-08
    • 1970-01-01
    • 2018-02-18
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多