初探 copy mutableCopy 1

copy:得到被拷贝对象的一个不可变类型的拷贝。

mutableCopy:得到被拷贝对象的一个可变类型的拷贝。

先看段代码:

NSLog(@"********************************************");
        NSLog(@"     ****** NSArray ******");
        id array0 ;
        id array1 ;
        NSArray *array = [NSArray arrayWithObjects:@"xiao",nil];
        array0 = [array copy];
        array1 = [array mutableCopy];
        
        NSLog(@"NSArray      %p",array);
        NSLog(@"copy         %p - %@",array0,[array0 class]);
        NSLog(@"mutableCopy  %p - %@",array1,[array1 class]);
        
        NSLog(@"********************************************");
       
        NSLog(@"     ****** NSMutableArray ******");
        id marray0 ;
        id marray1 ;
        NSMutableArray *marray = [NSMutableArray array];
        [marray addObject:@"zhangsan"];
        marray0 = [marray copy];
        marray1 = [marray mutableCopy];
        
        NSLog(@"NSMutableArray %p",marray);
        NSLog(@"copy           %p - %@",marray0,[marray0 class]);
        NSLog(@"mutableCopy    %p - %@",marray1,[marray1 class]);
        
        NSLog(@"********************************************");
        
        NSLog(@"     ****** NSString ******");
        NSString *string = @"text";
        NSString *copyString = [string copy];
        NSString *mutableCopyString = [string mutableCopy];
        NSLog(@"string      %p",string);
        NSLog(@"copyString  %p",copyString);
        NSLog(@"mutableCopy %p",mutableCopyString);
        
        NSLog(@"********************************************");
        
        NSLog(@"     ****** 自定义类 ******");
        Patient *zhangSan = [[Patient alloc] init];
        [zhangSan setName:@"zhangSan"];
        id patient0 = zhangSan ;
        id patient1 = [zhangSan copy];
        id patient2 = [zhangSan mutableCopy];
        NSLog(@"Patient     %p",zhangSan);
        NSLog(@"=           %p",patient0);
        NSLog(@"copy        %p",patient1);
        NSLog(@"mutableCopy %p",patient2);
        NSLog(@"********************************************");
        NSLog(@"自定义类的copy/mutableCopy 取决与 copyWithZone:/mutableCopyWithZone: 如何覆写");

@implementation Patient

-(instancetype)initWithName:(NSString *)name{
    self = [super init];
    if (self) {
        _name = name ;
    }
    return self ;
}

-(id)copyWithZone:(NSZone *)zone{
    return  self ;
    //return [[[self class] allocWithZone:zone] initWithName:_name];
}

-(id)mutableCopyWithZone:(NSZone *)zone{
    return [[[self class] allocWithZone:zone] initWithName:_name];
}

-(NSString *)description{
    return [NSString stringWithFormat:@"Name = %@",_name];
}
@end


输出为:


在输出中打印了每个变量在内存中的地址,和部分变量的类型。通过打印出来的信息可以得到这样的结论:

  • 无论被拷贝对象是可变类型的还是不可变类型的,使用copy会得到一个不可变类型的对象,mutableCopy会得到一个可变类型的对象
  • 如果被拷贝对象是不可变类型,对其发送copy:消息,相当于浅拷贝(指针拷贝),即得到的对象和被拷贝对象地址相同,其实是同一个对象。
  • 如果被拷贝对象是不可变类型,对其发送mutableCopy:消息,会得到一个内容如被拷贝对象相同的可变类型。因为得到的类型与被拷贝的对象类型不同,所以两者在内村中的地址肯定不同。
  • 如果被拷贝对象是可变类型的,对其发送copy:或者mtableCopy:消息相当于深拷贝,即得到一个跟被拷贝对象地址不同的对象,一个新对象。
  • 在自己定义的类型中,建议遵循上述原则。


再看一段代码:
NSLog(@"********************************************");
        Patient *p01 = [[Patient alloc] initWithName:@"aa"];
        Patient *p02 = [[Patient alloc] initWithName:@"bb"];
        NSMutableArray *mutableArray = [NSMutableArray array];
        [mutableArray addObject:p01];
        [mutableArray addObject:p02];
        Patient *receivePatient = [mutableArray objectAtIndex:0];
        [receivePatient setName:@"Changed"];
        NSLog(@"%@",[mutableArray objectAtIndex:0]);
        NSLog(@"mutable %p",[mutableArray objectAtIndex:0]);
        [mutableArray removeObjectAtIndex:0];
        NSLog(@"p01     %p",p01);
        NSLog(@"receive %p",receivePatient);
        
        NSLog(@"********************************************");
        
        Patient *p1 = [[Patient alloc] initWithName:@"a"];
        Patient *p2 = [[Patient alloc] initWithName:@"b"];
        NSArray *iarray = [NSArray arrayWithObjects:p1,p2,nil];
        Patient *tmpPatient = [iarray objectAtIndex:0];
        [tmpPatient setName:@"Changed"];
        NSLog(@"%@",[iarray objectAtIndex:0]);
        NSLog(@"array %p",[iarray objectAtIndex:0]);
        iarray = nil ;
        NSLog(@"p1    %p",p1);
        NSLog(@"tmp   %p",tmpPatient);
        NSLog(@"********************************************");
        NSLog(@"无论从可变数组还是从不可变数组取对象,都是取得了数组中对象的指针,但是该对象retain+1。");
        NSLog(@"[[iarray objectAtIndex:0]mutableCopy] / [[iarray objectAtIndex:0]copy] / [[mutableArray objectAtIndex:0]mutableCopy] / [[mutableArray objectAtIndex:0]copy] 会是什么样的结果呢?”);

输出为:


从输出可以看出:
  • 无论是在可变数组中还是不可变数组中增加对象,加入到数组中的都是指向该对象的指针(改变任一对象的name属性值后,所有对象的属性值都发生改变了)。
  • 被加入到数组的对象 retain count +1(对一个对象release一次,其他对象没有被释放掉)。 
  • 接收数组中某个对象一次,就对该对象retain count +1 。
  • id obj = [Objects objectAtIndex:i];  <=>  id obj = Object ;  指针赋值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值