如何让自己的类用 copy 修饰符?如何重写带 copy 关键字的 setter

    xiaoxiao2024-07-25  7

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

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

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

     

     

    具体步骤:

    需声明该类遵从 NSCopying 协议实现 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];

        }

    }

    转载请注明原文地址: https://ju.6miu.com/read-1291035.html
    最新回复(0)