《Effective Objective-C 2.0 》 阅读笔记 item22

第22条:理解NSCopying协议

1. NSCopying协议

如果想令自己的类支持拷贝操作,必须实现NSCopying协议,该协议只有一个方法:

- (id)copyWithZone:(NSZone *)zone

如果没实现该协议,会抛出异常,如:

Snip20160319_34.png

2. 例子

EOCPerson类

/* EOCPerson头文件 */
#import <Foundation/Foundation.h>

@interface EOCPerson : NSObject<NSCopying>  // 遵守NSCopying协议
@property(nonatomic ,copy, readonly) NSString *firstName;
@property(nonatomic ,copy, readonly) NSString *lastName;

- (id)initWithFirstName:(NSString *)firstName
            andLastName:(NSString *)lastName;
- (void)addFriend:(EOCPerson *)person;
- (void)removeFriend:(EOCPerson *)person;

@end

/* EOCPerson实现文件 */
#import "EOCPerson.h"

@implementation EOCPerson{
    // 实例变量
    NSMutableSet *_friends;
}

- (id)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName{
    if (self = [super init]) {
        _firstName = firstName;
        _lastName = lastName;
        _friends = [NSMutableSet new]; // 延迟初始化
    }
    return self;
}

- (void)addFriend:(EOCPerson *)person{
    [_friends addObject:person];
}

- (void)removeFriend:(EOCPerson *)person{
    [_friends removeObject:person];
}

// 要想该类能够实现拷贝操作必须实现copyWithZone:方法
- (id)copyWithZone:(NSZone *)zone{
    // 拷贝对象
    EOCPerson *copy = [[[self class] allocWithZone:zone]
                       initWithFirstName:_firstName
                       andLastName:_lastName];
    // 拷贝对象的实例变量
    copy->_friends = [_friends mutableCopy];
    return copy;
}

- (NSString *)description{
    return [NSString stringWithFormat:@"%@ %@", _firstName, _lastName];
}
@end

main函数

#import <Foundation/Foundation.h>
#import "EOCPerson.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        EOCPerson *person = [[EOCPerson alloc] initWithFirstName:@"Bob" andLastName:@"Smith"];
        EOCPerson *person2 = [[EOCPerson alloc] initWithFirstName:@"Bill" andLastName:@"Jobs"];
        EOCPerson *person3 = [[EOCPerson alloc] initWithFirstName:@"Scot" andLastName:@"Hotway"];
        EOCPerson *person4 = [person copy];
        [person4 addFriend:person2];
        [person4 addFriend:person3];
        NSLog(@"person4 = %@",person4);
    }
    return 0;
}

输出结果:

person4 = Bob Smith

3. 深拷贝与浅拷贝

  • 深拷贝:在拷贝对象自身时,将其底层数据也一并复制过去。
  • 浅拷贝:只拷贝collection容器对象本身,而不复制其中数据。

在自定义的类中,通常以浅拷贝的方式实现“copyWithZone:”方法。若想实现深拷贝,需要增加一个执行深拷贝的方法。NSSet类提供了下面这个初始化方法,用以执行深拷贝,如:

- (instancetype)initWithSet:(NSSet<ObjectType> *)set copyItems:(BOOL)flag

在上面的EOCPerson例子中,若需要深拷贝的话,则需要编写一个专供深拷贝所用的方法:

- (id)deepCopy{
    EOCPerson *copy = [[[self class] alloc]
                       initWithFirstName:_firstName
                       andLastName:_lastName];
    copy->_friends = [[NSMutableSet alloc] initWithSet:_friends copyItems:YES];
    return copy;
}

要点

  • 若想令自己所写的对象具有拷贝功能,则需实现NSCopying协议。
  • 如果自定义的对象分为可变版本与不可变版本,那么就要同时实现NSCopying与NSMutableCopying协议。
  • 复制对象时需决定采用浅拷贝还是深拷贝,一般情况下应该尽量执行浅拷贝。
  • 如果你所写的对象需要深拷贝,那么可考虑新增一个专门执行深拷贝的方法。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容