09.Objective-C Super,superClass,synthesize,@dynamic实质

问题

  1. super的实质
    2.superClass 的实质
    3.@synthesize 的实质
    4.@dynamic的实质

答案

  1. super的实质
NSLog(@"[super class] = %@", [super class]);
NSLog(@"[super superclass] = %@", [super superclass]);

- (Class)class
{
    return object_getClass(self);
}

- (Class)superclass
{
    return class_getSuperclass(object_getClass(self));
}

2.superClass 的实质

3.@synthesize 的实质
关联属性 和 成员变量

@synthesize age = _age, height = _height;

- (void)setAge:(int)age
{
    _age = age;
}

- (int)age
{
    return _age;
}

4.@dynamic的实质
提醒编译器,不需要自动生产setter,getter方法的实现,不用自动生成成员变量

// 提醒编译器不要自动生成setter和getter的实现、不要自动生成成员变量
@dynamic age;

void setAge(id self, SEL _cmd, int age)
{
    NSLog(@"age is %d", age);
}

int age(id self, SEL _cmd)
{
    return 120;
}

+ (BOOL)resolveInstanceMethod:(SEL)sel
{
    if (sel == @selector(setAge:)) {
        class_addMethod(self, sel, (IMP)setAge, "v@:i");
        return YES;
    } else if (sel == @selector(age)) {
        class_addMethod(self, sel, (IMP)age, "i@:");
        return YES;
    }
    return [super resolveInstanceMethod:sel];
}

你可能感兴趣的:(09.Objective-C Super,superClass,synthesize,@dynamic实质)