合成子类'变量时发生了什么?

时间:2016-01-09 08:14:45

标签: objective-c inheritance

我有一个超类和一个子类。我可以通过子类中的some_property访问变量self.some_property(在超类中声明)。

但是,如果我尝试直接使用_some_property访问实例变量,我会收到错误'Use of undeclared identifier _some_property...'

使用@synthesize some_property = _some_property使此警告无效。

当我重新合成这个属性时会发生什么?

1 个答案:

答案 0 :(得分:3)

您正在创建名为_some_property另一个 ivar - 并且还会覆盖getter方法以返回此新ivar的值。编译器给出了关于此的错误,如果基类的@implementation(即_some_property ivar的隐式声明)在@synthesize的站点上可见在子类中。

(顺便说一下,不要这样做!)

您可以通过检查Obj-C运行时向自己演示:

#import <Foundation/Foundation.h>
#import <objc/runtime.h>

@interface Base : NSObject
@property id foo;
@end


@interface Derived : Base
@end

@implementation Derived

@synthesize foo=_foo; // the compiler doesn't know about Base's _foo yet, so this is OK...

- (instancetype)init {
    if ((self = [super init])) {
        _foo = @"I'm derived";
    }
    return self;
}
@end

@implementation Base  // after Derived to avoid the error
- (instancetype)init {
    if ((self = [super init])) {
        _foo = @"I'm base";
    }
    return self;
}
@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        Derived *obj = [Derived new];
        NSLog(@"getter returns %@", obj.foo);

        unsigned int count = 0;

        // Examine Base ivars
        NSLog(@"Base ivars:");
        Ivar *ivars = class_copyIvarList([Base class], &count);
        for (unsigned int i = 0; i < count; i++) {
            NSLog(@" %s = %@", ivar_getName(ivars[i]), object_getIvar(obj, ivars[i]));
        }

        // Examine Derived ivars
        NSLog(@"Derived ivars:");
        ivars = class_copyIvarList([Derived class], &count);
        for (unsigned int i = 0; i < count; i++) {
            NSLog(@" %s = %@", ivar_getName(ivars[i]), object_getIvar(obj, ivars[i]));
        }
    }
    return 0;
}

输出:

getter returns I'm derived
Base ivars:
 _foo = I'm base
Derived ivars:
 _foo = I'm derived
相关问题