美文网首页
iOS 属性的重写问题

iOS 属性的重写问题

作者: iOS_肖晨 | 来源:发表于2018-02-11 13:46 被阅读254次

    问题简介

    当子类重写父类属性的时候会出现如下警告:
    Auto property synthesis will not synthesize property 'name'; it will be implemented by its superclass, use @dynamic to acknowledge intention

    产生原因

    在 OC 中属性就是给一个类的成员变量提供封装:
    Objective-C properties offer a way to define the information that a class is intended to encapsulate。
    通过声明属性,我们可以很简单的为一个成员变量定义其是否是只读的还是读写的,是否是原子操作的等等特性,也就是说如果说封装是为成员变量套了一层壳的话,那么 @property关键字做的事情就是预定义这层壳是个什么样子的壳,然后通过 @sythesize关键字生成真正的壳并把这个壳套在实际的成员变量上(如果没有定义这个成员变量该关键字也可以自动生成对应的成员变量)。当然这层壳包括了自动生成的 get set 方法。
    在最开始的时候,我们在代码中写了@property对应的就要写一个@sythesize,在苹果使用了 LLVM 作为编译器以后,如果我们没有写 @sythesize,编译器就会为我们自动的生成一个 @sythesize property = _property。这个特性叫做 Auto property synthesize
    现在就能看到问题产生的原因,当我们想覆盖父类的属性的时候,Auto property synthesize就不工作了,子类也就没有属性所对应的成员变量了。

    解决方式

    1. 子类添加@synthesize name = _name;方法,系统自动帮子类生成 get 和 set 方法。
    2. 子类添加@dynamic name;方法,在类拓展 Extension 中添加成员变量NSString *_name;并实现 get 和 set 方法
    - (void)setName:(NSString *)name {
        _name = name;
    }
    - (NSString *)name {
        return _name;
    }
    

    我们可以通过运行时方法查看属性及成员变量有没有添加成功,代码如下:

    unsigned int ivarCount;
    Ivar *ivarList = class_copyIvarList([Student class], &ivarCount);
    for (NSUInteger i = 0; i < ivarCount; i++) {
        Ivar ivar = ivarList[i];
        NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];
        NSLog(@"Student - ivarName: %@", ivarName);
    }
    
    unsigned int propertyCount;
    objc_property_t *propertyList = class_copyPropertyList([Student class], &propertyCount);
    for (NSInteger i = 0; i < propertyCount; i++) {
        objc_property_t property = propertyList[i];
        NSString *propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
        NSLog(@"Student - propertyName: %@", propertyName);
    }
    

    相关文章

      网友评论

          本文标题:iOS 属性的重写问题

          本文链接:https://www.haomeiwen.com/subject/mbjktftx.html