谈谈IOS开发的NSCopying和NSMutableCopying协议的理解和基本使用

来源:互联网 发布:软件开发必备工具 编辑:程序博客网 时间:2024/05/19 14:39
看完NSCopying和NSMutableCopying的文章后,自己总结了一下,形成文档,方便后续完善,也希望能够帮助这方便有疑惑的小伙伴。

首先,必须明确并不是所有的类都支持Copy协议的,也就说大部分的类并不支持复制操作(Copy),但是常见的NSString、NSArray、NSDictionary等还是支持复制操作的。

至于NSCopying和NSMutableCopying的区别,简单来说,就是NSMutableCopying复制的数据支持可变性。

附上我的代码,后面我会稍微说明下

NDYTestCopyFirstObject.h
#import <Foundation/Foundation.h>@interface NDYTestCopyFirstObject : NSObject<NSCopying,NSMutableCopying>@property (nonatomic, assign) UInt8 age;@property (nonatomic, strong) NSString *userName;@property (nonatomic, strong) NSString *address;@property (nonatomic, strong) NSMutableArray *theFriends;@property (nonatomic, strong) NSMutableString *theMutableStr;@end

NDYTestCopyFirstObject.m
#import "NDYTestCopyFirstObject.h"@implementation NDYTestCopyFirstObject- (id)init {    self = [super init];    if (self) {        self.theFriends = [NSMutableArray arrayWithObjects:@"first",@"second",@"third", nil];        self.theMutableStr = [NSMutableString stringWithString:@"Hello"];    }    return self;}- (id)copyWithZone:(NSZone *)zone {    NDYTestCopyFirstObject *instance = [[NDYTestCopyFirstObject alloc] init];    if (instance) {        instance.age = self.age;        instance.userName = [self.userName copyWithZone:zone];        instance.address = [self.address copyWithZone:zone];        instance.theFriends = [self.theFriends copyWithZone:zone];        instance.theMutableStr = [self.theMutableStr copyWithZone:zone];    }    return instance;}- (id)mutableCopyWithZone:(NSZone *)zone {    NDYTestCopyFirstObject *instance = [[NDYTestCopyFirstObject alloc] init];    if (instance) {        instance.age = self.age;        instance.userName = [self.userName mutableCopyWithZone:zone];        instance.address = [self.address mutableCopyWithZone:zone];        instance.theFriends = [self.theFriends mutableCopyWithZone:zone];        instance.theMutableStr = [self.theMutableStr mutableCopyWithZone:zone];    }    return instance;}- (NSString *)description {    NSString *dis = [NSString stringWithFormat:@"\nuserName:%@  address:%@  age:%d %@ friends:%@\n",self.userName,self.address,self.age,self.theMutableStr,self.theFriends];    return dis;}
</pre><pre name="code" class="objc">具体使用    NDYTestCopyFirstObject *obj1 = [[NDYTestCopyFirstObject alloc] init];    obj1.userName = @"first name";    obj1.address = @"first address";    obj1.age = 15;            NDYTestCopyFirstObject *obj2 = [obj1 mutableCopy];//    NDYTestCopyFirstObject *obj2 = [obj1 copy];    NSLog(@"%@%@%p    %p\n",obj1,obj2,&obj1,&obj2);    obj2.userName = @"second name";//    obj2.age = 25;//    obj2.address = @"second address";        //Mutable    [obj2.theFriends addObject:@"fourth"];    [obj2.theMutableStr appendString:@" World"];        NSLog(@"%@%@%p    %p\n",obj1,obj2,&obj1,&obj2);


说明: 如果你只是想复制某个对象,而且不会改变对象的可变元素(比如NSMutableArray增加或者删除元素),建议直接使用NSCopying协议就行了;
       但是如果想对复制的对象的某些可变元素做修改(比如NSMutableArray增加或者删除元素),必须使用NSMutableCopying协议;

           实现NSCopying协议必须实现方法: - (id)copyWithZone:(NSZone *)zone

           实现NSMutableCopying协议必须实现方法: - (id)mutableCopyWithZone:(NSZone *)zone

0 1
原创粉丝点击