开发者

Referring to Objective-C superclass variable

开发者 https://www.devze.com 2023-04-05 19:55 出处:网络
I have a base class with some variables defined that looks something like: @interface Foo : NSObject {

I have a base class with some variables defined that looks something like:

@interface Foo : NSObject {
    SomeObject *baz;
}

@implementation Foo
@synthesize baz;

// ...

@end

@interface Bar : Foo
@end

@implementation Bar

-(void)someMethod {
    NSString *foostr = [NSString stringWithFormat:"%@", baz];
}

I actually have a base class with about 30 subclasses.

The issue is that in most of them I can reference the base class' varia开发者_JAVA百科ble bad simply as baz but in a couple of sub-classes I have to explicitly reference bad as self.baz ...

Has anybody else seen this pathology?


If your compiler is set to gcc, there's a bug: A subclass that defines a @property without a corresponding ivar definition (which is perfectly legal) hides any superclass ivars.

Solution: Switch your compiler to LLVM.

(If that solution isn't feasible for some reason, the fallback is what you discovered: Use messaging instead of direct reference.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号