• NSCopy&NSMutableCopy


    struct student
    {
        int a;
    
        float f;
    
        char c;
    
        long l;
    };
    
    struct person
    {
        int a;
    
        float f;
    
        char c;
    
        long l;
    
    };
    void test1(void) { int a=12; int b; memcpy(&b, &a , sizeof(a)); NSLog(@"b=%i",b); struct student stu1={1,1.5,'x',88888888}; struct student stu2; memcpy(&stu2, &stu1, sizeof(stu1)); NSLog(@"%i, %f,%c, %ld",stu2.a,stu2.f,stu2.c,stu2.l); // struct person per=stu1; struct person per; memcpy(&per, &stu1, sizeof(stu1)); NSLog(@"per: %i,%f,%c,%ld",per.a,per.f,per.c,per.l); }
    void testStringCopy(void) { NSString* s1=[[NSString alloc]initWithFormat:@"name:%s","wenhua" ]; NSString* s2 = [s1 copy]; //浅拷贝 NSLog(@"s1:%p, %@",s1,s1); NSLog(@"s2:%p, %@",s2,s2); NSMutableString* s3 = [s1 copy]; NSLog(@"s3:%p,%@",s3,s3); // 不可变字符串的copy方法: 1,浅拷贝,2,还是不可变 NSString* s4 = [s1 mutableCopy]; //深拷贝 NSLog(@"s4:%p,%@",s4,s4); [(__bridge id)(__bridge void *)s4 appendString:@"append"]; NSLog(@"s4:%p,%@",s4,s4); NSLog(@"s1:%p, %@",s1,s1); // 不可变字符串的mutableCopy方法: 1,深拷贝,2,拷贝出来的是可变的 NSMutableString* s5=[[NSMutableString alloc]initWithString:@"string"]; NSMutableString* s6 = [s5 copy]; //深拷贝,但s6不可变 NSLog(@"s5: %p,%@",s5,s5); NSLog(@"s6: %p,%@",s6,s6); // [s6 appendString:@"appendStringFors6"]; // 可变字符串的copy方法: 1,深拷贝,2,,拷贝出来的是不可变的 NSMutableString* s7 = [s5 mutableCopy];//深拷贝,s7是可变的 NSLog(@"s7: %p,%@",s7,s7); [s7 appendString:@"appendStringFors7"]; NSLog(@"s7: %p,%@",s7,s7); // 可变字符串的mutableCopy方法: 1,深拷贝, 2,拷贝出来的是可变的 /* 总结: 拷贝 可变拷贝 copy mutableCopy 不可变字符串NSString调: 浅,不可变 深,可变 可变NSMutableString调: 深,不可变 深,可变     你想啊,mutableCopy后,为了两个对象修改后互不影响,一定是深拷贝啦,不会只是拷贝指针;可变字符串copy后,自然是深拷贝,因为一个可变一个不可变;不可变字符串copy,只需要浅拷贝就行喽! */ } void testArrayCopy(void) { /* 不可变数组和可变数组的拷贝情况: 不可变字典和可变字典的拷贝情况: */ NSString* s1=@"abcd"; // NSArray* a1=@[@"1",@"2"]; NSMutableArray* a1 = [[ NSMutableArray alloc]initWithArray:@[@"1",@"2"]]; Person* person = [[ Person alloc]init]; NSLog(@"person计数:%lu",[person retainCount]); [a1 addObject:person]; NSLog(@"person计数:%lu",[person retainCount]); NSArray* array1 = [[NSArray alloc]initWithObjects:s1,a1, nil]; NSArray* array2 =[array1 copy]; NSLog(@"array1:%p,%@",array1,array1); NSLog(@"array2:%p,%@",array2,array2); NSMutableArray *array3 = [array1 copy]; NSLog(@"array3:%p,%@",array3,array3); NSMutableArray * array4 = [array1 mutableCopy]; //可变 NSLog(@"array4:%p,%@",array4,array4); NSLog(@"array1:[0]:%p,[1]:%p",array1[0],array1[1]); NSLog(@"array4:[0]:%p,[1]:%p",array4[0],array4[1]); [array4 addObject:@"add"]; NSLog(@"array4 count is %lu",[array4 count]);//array4是可变数组 [array1 release]; [array2 release]; [array3 release]; [array4 release]; [person release]; [a1 release]; } void test3(void) { Person *person =[[Person alloc]init]; NSLog(@"person的计数:%lu",[person retainCount]); NSMutableArray *array = [[ NSMutableArray alloc]init]; [array addObject:person]; NSLog(@"person的计数:%lu",[person retainCount]); NSMutableArray *newArray = [array mutableCopy];//拷贝了一层 NSLog(@"person的计数:%lu",[person retainCount]); [array release]; [newArray release]; [person release]; } void test4(void) { Person* person = [[ Person alloc]init]; NSLog(@"person的计数:%lu",[person retainCount]); NSMutableArray *array = [[ NSMutableArray alloc]init]; [array addObject:person]; NSLog(@"person的计数:%lu",[person retainCount]); //对可变数组调copy方法, NSMutableArray *newArray = [array copy]; // ? 拷贝出来的数组可不可变? 拷了几层? // [newArray addObject:@"add"]; //回答:拷贝出来的数组不可变 NSLog(@"person的计数:%lu",[person retainCount]); [array release]; [newArray release]; [person release]; }
    void test2(void) { Person* person = [[ Person alloc]init]; //问:为什么NSString类的实例变量在property语法中要用copy? NSMutableString* name=[[NSMutableString alloc]initWithString:@"wenhua"]; person.name=name; NSLog(@"person.name:%p,name:%p",person.name,name); [name appendFormat:@".奥斯特洛夫斯基"]; NSLog(@"person.name: %@",person.name); //因为传入person对象的name是可变字符串对象,如果用retain来修饰,则会造成在外边就可以修改person对象的实例变量,不符合封装的思想,所以用copy } //拷贝自定义的对象 void copyForObject(void) { /* 理论: 1, 对象如果要调copy方法,就要遵守NSCopy协议 2, 对象如果要调mutableCopy方法,就要遵守NSMutableCopying协议 但是注意: copy方法 不是协议NSCopying的方法 mutableCopy方法 也不是协议NSMutableCopying的方法 copy和mutableCopy都是NSObject实现的方法 因为: 在NSObject实现的copy方法内部调用了协议NSCopying的方法 在NSObject实现的mutableCopy方法内部调用了协议NSMutableCopying的方法 NSCopying规定的方法名: copyWithZone: NSMutableCopying规定的方法名:       mutableCopyWithZone: */ /* #import "Person.h" @implementation Person //协议NSCopying规定的一个方法,这个方法由copy方法内部来调 -(id)copyWithZone:(NSZone *)zone { Person* person = [[[self class] allocWithZone:zone ]init]; person.name=self.name; return person; } -(id)mutableCopyWithZone:(NSZone *)zone { Person* person = [[[self class] allocWithZone:zone ]init]; person.name=self.name; return person; } @end */ Person* p1 = [[ Person alloc]init]; Person *p2 = [p1 copy];//p1可以调copy,是因为Person类实现了copyWithZone:方法 NSLog(@"p1:%p,p2:%p",p1,p2); Person* p3 = [ p1 mutableCopy];//p1可以调mutableCopy,是因为Person类实现了mutableCopyWithZone:方法 NSLog(@"p3:%p",p3); }

    父类中实现子类的深拷贝

    - (id)copyWithZone:(NSZone *)zone {  
        id copyInstance = [[[self class] allocWithZone:zone] init];  
        size_t instanceSize = class_getInstanceSize([self class]);  
        memcpy((__bridge voidvoid *)(copyInstance), (__bridge const voidvoid *)(self), instanceSize);  
        return copyInstance;  
    }

     

    //***********************************************

    关于容器实现copy或mutableCopy,容器内元素默认都是指针拷贝

    除非使用

    NSArray *trueDeepCopyArray = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:array]];

    实现元素的内容拷贝,深拷贝.

    copy和mutableCopy对容器本身是深拷贝,其内部元素是指针拷贝.

    //类对象调用copy后,为深拷贝,但是类中属性为指针拷贝

            ApplePie *pie = [[ApplePie alloc]init];
            pie.name = @"I am an apple pie";
            
            ApplePie *ala = [pie copy];
            
            NSLog(@"%p %p",ala.name,pie.name);// 相同的地址
            NSLog(@"%p %p",ala,pie);// 不同的地址

    引用:http://www.cnblogs.com/tangbinblog/p/3964003.html

  • 相关阅读:
    verdi issues on license
    geci
    组合数据类型练习
    熟悉常用的Linux操作
    1.大数据概述
    c语言文法分析
    词法分析器#include<stdio.h> #include<string.h> #include<iostream.h> char prog[80],token[8]; char ch; int syn,p,m=0,n,row,sum=0; char *rwtab[6]={"begin","if","then","while","do","end"
    关于编译原理
    可变参数
    函数和指针
  • 原文地址:https://www.cnblogs.com/hyuganatsu/p/NSCopy_NSMutableCopy.html
Copyright © 2020-2023  润新知