zoukankan      html  css  js  c++  java
  • 如何让自己的类用 copy 修饰符?如何重写带 copy 关键字的 setter?

    出题者简介: 孙源(sunnyxx),目前就职于百度

    整理者简介:陈奕龙,目前就职于滴滴出行。

    转载者:豆电雨(starain)微信:doudianyu

    若想令自己所写的对象具有拷贝功能,则需实现 NSCopying 协议。如果自定义的对象分为可变版本与不可变版本,那么就要同时实现 NSCopying 与 NSMutableCopying 协议。

    具体步骤:

    1. 需声明该类遵从 NSCopying 协议
    2. 实现 NSCopying 协议。该协议只有一个方法:

      - (id)copyWithZone:(NSZone *)zone;

      注意:一提到让自己的类用 copy 修饰符,我们总是想覆写copy方法,其实真正需要实现的却是 “copyWithZone” 方法。

    以第一题的代码为例:

        // .h文件
        // http://weibo.com/luohanchenyilong/
        // https://github.com/ChenYilong
        // 修改完的代码
    
        typedef NS_ENUM(NSInteger, CYLSex) {
            CYLSexMan,
            CYLSexWoman
        };
    
        @interface CYLUser : NSObject<NSCopying>
    
        @property (nonatomic, readonly, copy) NSString *name;
        @property (nonatomic, readonly, assign) NSUInteger age;
        @property (nonatomic, readonly, assign) CYLSex sex;
    
        - (instancetype)initWithName:(NSString *)name age:(NSUInteger)age sex:(CYLSex)sex;
        + (instancetype)userWithName:(NSString *)name age:(NSUInteger)age sex:(CYLSex)sex;
    
        @end

    然后实现协议中规定的方法:

    - (id)copyWithZone:(NSZone *)zone {
        CYLUser *copy = [[[self class] allocWithZone:zone] 
                         initWithName:_name
                                      age:_age
                                      sex:_sex];
        return copy;
    }

    但在实际的项目中,不可能这么简单,遇到更复杂一点,比如类对象中的数据结构可能并未在初始化方法中设置好,需要另行设置。举个例子,假如 CYLUser 中含有一个数组,与其他 CYLUser 对象建立或解除朋友关系的那些方法都需要操作这个数组。那么在这种情况下,你得把这个包含朋友对象的数组也一并拷贝过来。下面列出了实现此功能所需的全部代码:

    // .h文件
    // http://weibo.com/luohanchenyilong/
    // https://github.com/ChenYilong
    // 以第一题《风格纠错题》里的代码为例
    
    typedef NS_ENUM(NSInteger, CYLSex) {
        CYLSexMan,
        CYLSexWoman
    };
    
    @interface CYLUser : NSObject<NSCopying>
    
    @property (nonatomic, readonly, copy) NSString *name;
    @property (nonatomic, readonly, assign) NSUInteger age;
    @property (nonatomic, readonly, assign) CYLSex sex;
    
    - (instancetype)initWithName:(NSString *)name age:(NSUInteger)age sex:(CYLSex)sex;
    + (instancetype)userWithName:(NSString *)name age:(NSUInteger)age sex:(CYLSex)sex;
    - (void)addFriend:(CYLUser *)user;
    - (void)removeFriend:(CYLUser *)user;
    
    @end

    // .m文件

    // .m文件
    // http://weibo.com/luohanchenyilong/
    // https://github.com/ChenYilong
    //
    
    @implementation CYLUser {
        NSMutableSet *_friends;
    }
    
    - (void)setName:(NSString *)name {
        _name = [name copy];
    }
    
    - (instancetype)initWithName:(NSString *)name
                             age:(NSUInteger)age
                             sex:(CYLSex)sex {
        if(self = [super init]) {
            _name = [name copy];
            _age = age;
            _sex = sex;
            _friends = [[NSMutableSet alloc] init];
        }
        return self;
    }
    
    - (void)addFriend:(CYLUser *)user {
        [_friends addObject:user];
    }
    
    - (void)removeFriend:(CYLUser *)user {
        [_friends removeObject:user];
    }
    
    - (id)copyWithZone:(NSZone *)zone {
        CYLUser *copy = [[[self class] allocWithZone:zone]
                         initWithName:_name
                         age:_age
                         sex:_sex];
        copy->_friends = [_friends mutableCopy];
        return copy;
    }
    
    - (id)deepCopy {
        CYLUser *copy = [[[self class] allocWithZone:zone]
                         initWithName:_name
                         age:_age
                         sex:_sex];
        copy->_friends = [[NSMutableSet alloc] initWithSet:_friends
                                                 copyItems:YES];
        return copy;
    }
    
    @end
    

    以上做法能满足基本的需求,但是也有缺陷:

    如果你所写的对象需要深拷贝,那么可考虑新增一个专门执行深拷贝的方法。

    【注:深浅拷贝的概念,在下文中有介绍,详见下文的:用@property声明的 NSString(或NSArray,NSDictionary)经常使用 copy 关键字,为什么?如果改用 strong 关键字,可能造成什么问题?

    在例子中,存放朋友对象的 set 是用 “copyWithZone:” 方法来拷贝的,这种浅拷贝方式不会逐个复制 set 中的元素。若需要深拷贝的话,则可像下面这样,编写一个专供深拷贝所用的方法:

    - (id)deepCopy {
        CYLUser *copy = [[[self class] allocWithZone:zone]
                         initWithName:_name
                         age:_age
                         sex:_sex];
        copy->_friends = [[NSMutableSet alloc] initWithSet:_friends
                                                 copyItems:YES];
        return copy;
    }
    

    至于如何重写带 copy 关键字的 setter这个问题,

    如果抛开本例来回答的话,如下:

    - (void)setName:(NSString *)name {
        //[_name release];
        _name = [name copy];
    }

    不过也有争议,有人说“苹果如果像下面这样干,是不是效率会高一些?”

    - (void)setName:(NSString *)name {
        if (_name != name) {
            //[_name release];//MRC
            _name = [name copy];
        }
    }

    这样真得高效吗?不见得!这种写法“看上去很美、很合理”,但在实际开发中,它更像下图里的做法:

    enter image description here

    克强总理这样评价你的代码风格:

    enter image description here

    我和总理的意见基本一致:

    老百姓 copy 一下,咋就这么难?

    你可能会说:

    之所以在这里做if判断 这个操作:是因为一个 if 可能避免一个耗时的copy,还是很划算的。 (在刚刚讲的:《如何让自己的类用 copy 修饰符?》里的那种复杂的copy,我们可以称之为 “耗时的copy”,但是对 NSString 的 copy 还称不上。)

    但是你有没有考虑过代价:

    你每次调用 setX: 都会做 if 判断,这会让 setX: 变慢,如果你在 setX:写了一串复杂的 if+elseif+elseif+... 判断,将会更慢。

    要回答“哪个效率会高一些?”这个问题,不能脱离实际开发,就算 copy 操作十分耗时,if 判断也不见得一定会更快,除非你把一个“ @property他当前的值 ”赋给了他自己,代码看起来就像:

    [a setX:x1];
    [a setX:x1];    //你确定你要这么干?与其在setter中判断,为什么不把代码写好?

    或者

    [a setX:[a x]];   //队友咆哮道:你在干嘛?!!

    不要在 setter 里进行像 if(_obj != newObj) 这样的判断。(该观点参考链接: How To Write Cocoa Object Setters: Principle 3: Only Optimize After You Measure 

    什么情况会在 copy setter 里做 if 判断? 例如,车速可能就有最高速的限制,车速也不可能出现负值,如果车子的最高速为300,则 setter 的方法就要改写成这样:

    -(void)setSpeed:(int)_speed{
        if(_speed < 0) speed = 0;
        if(_speed > 300) speed = 300;
        _speed = speed;
    }

    回到这个题目,如果单单就上文的代码而言,我们不需要也不能重写 name 的 setter :由于是 name 是只读属性,所以编译器不会为其创建对应的“设置方法”,用初始化方法设置好属性值之后,就不能再改变了。( 在本例中,之所以还要声明属性的“内存管理语义”--copy,是因为:如果不写 copy,该类的调用者就不知道初始化方法里会拷贝这些属性,他们有可能会在调用初始化方法之前自行拷贝属性值。这种操作多余而低效)。

    那如何确保 name 被 copy?在初始化方法(initializer)中做:

        - (instancetype)initWithName:(NSString *)name 
                                     age:(NSUInteger)age 
                                     sex:(CYLSex)sex {
             if(self = [super init]) {
                _name = [name copy];
                _age = age;
                _sex = sex;
                _friends = [[NSMutableSet alloc] init];
             }
             return self;
        }
    
  • 相关阅读:
    Java hutool/java 常用方法
    版本控制工具 TortoiseGit 基本配置
    SpringBlade 码云上我自己的fork的仓库简单使用
    版本控制工具 Git SourceTree SSH 连接码云
    版本控制工具 Git SourceTree 报错 fatal: could not read Username for 'https://gitee.com': No such file or directory
    在充电桩联网部署方案中4G DTU的优势是什么
    4G DTU模块和串口设备连接的方式
    4g物联网模块的原理
    4G DTU采用的4G通信模块介绍
    4G DTU数据传输终端的功能介绍
  • 原文地址:https://www.cnblogs.com/starainDou/p/5253185.html
Copyright © 2011-2022 走看看