zoukankan      html  css  js  c++  java
  • copy 和 mutablecopy 笔记

        NSString *a = [[NSString alloc]initWithString:@"hello"];
        NSString *b = [a copy];
        NSLog(@"%d   %d",a.retainCount, b.retainCount);// 2,2
        
        NSMutableString *f = [a mutableCopy];
        
         NSLog(@"%d   %d",a.retainCount, f.retainCount);// 2,1
        
        [f appendString:@"dd"];
        NSLog(@"%@,%@",a,f);// hello,hellodd
        // 对于一个不可变对象来说 copy 是浅copy 只是指针复制 其retainCount+1
        // mutablecopy 是深copy 是对象复制
        
        NSMutableString *c = [[NSMutableString alloc]initWithString:@"hello"];
        NSMutableString *d = [c copy];
        NSLog(@"%d  %d",c.retainCount,d.retainCount);// 1,1
        //  [d appendString:@"ddd"]; //error 因为copy返回一个不可改变对象
        NSMutableString *e = [c mutableCopy];
         NSLog(@"%d  %d",c.retainCount,e.retainCount);// 1,1
        [e appendString:@"dddd"];
        NSLog(@"%@,%@",c,e);// hello,hellodddd
        // 对于可变对象来说 copy 和mutablecopy 都是深copy  都是拷贝对象  不过copy返回的对象是一个不可变对象
        //------------------------------------------------------------//
    //    对于系统容器类对象
        
         NSArray *array1 = [NSArray arrayWithObjects:@"a",@"b",@"c",nil];
        NSArray *array1Copy = [array1 copy];
        NSLog(@"%d, %d",array1.retainCount,array1Copy.retainCount);//2,2
        NSMutableArray *arrayMcopy = [array1 mutableCopy];
         NSLog(@"%d, %d",array1.retainCount,arrayMcopy.retainCount);//2,1
        [arrayMcopy removeObjectAtIndex:0];
        [arrayMcopy addObject:@"d"];
        NSLog(@"%@-----%@",array1,arrayMcopy);//a,b,c  -----b,c,d
         NSArray *mArray1 = [NSArray arrayWithObjects:[NSMutableString stringWithString:@"a"],@"b",@"c",nil];
        NSArray *mArray1copy = [mArray1 copy];
        NSLog(@"%d,%d",mArray1.retainCount,mArray1copy.retainCount);//2,2
        NSMutableArray *mArrayMcopy = [mArray1 mutableCopy];
        NSLog(@"%d,%d",mArray1.retainCount,mArrayMcopy.retainCount);//2,1
        NSMutableString *temp = [mArray1 objectAtIndex:0];
        NSLog(@"%@",temp);//a
        [temp appendString:@"aa"];
        NSLog(@"%@",mArray1);//aaa b c
        NSLog(@"%@",mArray1copy); //aaa b c
        NSLog(@"%@",mArrayMcopy);// aaa b c
        //对于容器而言,其元素对象始终是指针复制。如果需要元素对象也是对象复制,就需要实现深拷贝。
        NSArray* trueDeepCopyArray = [NSKeyedUnarchiver unarchiveObjectWithData:
                                      [NSKeyedArchiver archivedDataWithRootObject: mArray1]];
        NSMutableString *temp2 = [trueDeepCopyArray objectAtIndex:0];
        [temp2 appendString:@"aa"];
        
        NSLog(@"%@",mArray1);//aaa b c
        NSLog(@"%@",trueDeepCopyArray);//aaaaa b c
        //trueDeepCopyArray是完全意义上的深拷贝

  • 相关阅读:
    MySQL日志设置及查看方法
    delphi 程序强制结束自身(两种方法都暴力)
    BAT-把当前用户以管理员权限运行(用户帐户控制:用于内置管理员帐户的管理员批准模式)
    SetWindowLong
    修复VirtualBox "This kernel requires the following features not present on the CPU: pae Unable to boot – please use a kernel appropriate for your CPU"(安装深度Linux的时候就需要)
    国家网络与信息安全中心紧急通报:请升级安装补丁!
    Web Api 2(Cors)Ajax跨域访问
    理解依赖注入
    www.centos.org
    VMware Player安装centos
  • 原文地址:https://www.cnblogs.com/gaoxiao228/p/2464783.html
Copyright © 2011-2022 走看看