objective-c中的深浅拷贝

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了objective-c中的深浅拷贝相关的知识,希望对你有一定的参考价值。

浅复制:只复制指向对象的指针,而不复制引用对象本身。计数器+1 ,就像比如retain

深层复制:复制引用对象本身。计数器不变,产生新对象 

 

retain:始终是浅复制。引用计数每次加一。返回对象是否可变与被复制的对象保持一致。

 

copy:对于可变对象为深复制,引用计数不改变;对于不可变对象是浅复制,引用计数每次加一。始终返回一个不可变对象。

mutableCopy:始终是深复制,引用计数不改变。始终返回一个可变对象。

 

技术分享
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {


    NSMutableArray *array1=[NSMutableArray arrayWithObjects:@"two day", nil];
    NSMutableArray *array2=[array1 copy];
    NSArray *array3=[array2 copy];
    NSArray *array4=[array2 retain];
    NSMutableArray *array5=[array1 retain];
    NSMutableArray *array6=[array1 mutableCopy];
    NSArray *array7=[array2 mutableCopy];
    NSLog(@"-------------");
    NSLog(@"array1:%@ %p",array1, array1);
    NSLog(@"array2:%@ %p",array2, array2);
    NSLog(@"array3:%@ %p",array3, array3);
    NSLog(@"array4:%@ %p",array4, array4);
    NSLog(@"array5:%@ %p",array5, array5);
    NSLog(@"array6:%@ %p",array6, array6);
    NSLog(@"array7:%@ %p",array7, array7);
    NSLog(@"-------------");
    return 0;
}
源代码

 

技术分享
2015-04-01 20:46:23.131 04-01-person[2204:165655] -------------
2015-04-01 20:46:23.132 04-01-person[2204:165655] array1:(
    "two day"
) 0x100114570
2015-04-01 20:46:23.133 04-01-person[2204:165655] array2:(
    "two day"
) 0x100114700
2015-04-01 20:46:23.133 04-01-person[2204:165655] array3:(
    "two day"
) 0x100114700
2015-04-01 20:46:23.133 04-01-person[2204:165655] array4:(
    "two day"
) 0x100114700
2015-04-01 20:46:23.133 04-01-person[2204:165655] array5:(
    "two day"
) 0x100114570
2015-04-01 20:46:23.133 04-01-person[2204:165655] array6:(
    "two day"
) 0x1001150d0
2015-04-01 20:46:23.134 04-01-person[2204:165655] array7:(
    "two day"
) 0x100115b00
2015-04-01 20:46:23.134 04-01-person[2204:165655] -------------
Program ended with exit code: 0
运行结果

 

数组类型转换:

1、不可变对象→可变对象的转换:       

技术分享
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    NSArray *array01=[NSArray arrayWithObjects:@"zhang",@"wang",@"li", nil];
    
    NSMutableArray  *array02=[array01 mutableCopy]; //mutableCopy始终返回一个可变对象
    
    NSLog(@"array01: %@  %p",array01,array01);
    NSLog(@"array02: %@  %p",array02,array02);
    return 0;
}
源代码
技术分享
2015-04-01 21:00:28.218 04-01-person[2252:170414] array01: (
    zhang,
    wang,
    li
)  0x100106c80
2015-04-01 21:00:28.220 04-01-person[2252:170414] array02: (
    zhang,
    wang,
    li
)  0x1001076d0
Program ended with exit code: 0
运行结果

 

2、可变对象→不可变对象的转换:

技术分享
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    NSMutableArray *array1   = [NSMutableArray arrayWithObjects:@"zhang",@"wang",@"li", nil];
    NSArray *array2=[array1   copy];//copy对于可变对象为深复制,引用计数不改变;对于不可变对象是浅复制,引用计数每次加一。始终返回一个不可变对象
    NSLog(@"array1: %@  %p",array1,array1);
    NSLog(@"array2: %@  %p",array2,array2);
    return 0;
}
源代码
技术分享
2015-04-01 21:03:45.349 04-01-person[2263:171615] array1: (
    zhang,
    wang,
    li
)  0x100112900
2015-04-01 21:03:45.350 04-01-person[2263:171615] array2: (
    zhang,
    wang,
    li
)  0x1001133b0
运行结果

 

3、可变对象→可变对象的转换(不同指针变量指向不同的内存地址)

技术分享
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {

    NSMutableArray *array1= [NSMutableArray arrayWithObjects:@"zhang",@"wang",@"li", nil];
    
    NSMutableArray *array2=[array1 mutableCopy];
    NSLog(@"array1: %@  %p",array1,array1);
    NSLog(@"array2: %@  %p",array2,array2);
    return 0;
}
源代码
技术分享
2015-04-01 20:57:41.732 04-01-person[2243:169571] array1: (
    zhang,
    wang,
    li
)  0x100114570
2015-04-01 20:57:41.734 04-01-person[2243:169571] array2: (
    zhang,
    wang,
    li
)  0x100115020
Program ended with exit code: 0
运行结果

 

 

4、同类型对象之间的指针复制(不同指针变量指向同一块内存地址):

技术分享
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {


    NSArray *array1=[NSArray arrayWithObjects:@"one",@"two",@"three", nil];
    NSArray *array2=[array1 copy];
    NSLog(@"array1: %@  %p",array1,array1);
    NSLog(@"array2: %@  %p",array2,array2);
    return 0;
}
源代码
技术分享
2015-04-01 20:53:07.398 04-01-person[2217:167996] array1: (
    one,
    two,
    three
)  0x1006069a0
2015-04-01 20:53:07.399 04-01-person[2217:167996] array2: (
    one,
    two,
    three
)  0x1006069a0
Program ended with exit code: 0
运行结果

当多个指针同时指向同一块内存区域,那么这些个指针同时拥有对该内存区的所有权。所有权的瓜分过程,这时候就要用到浅拷贝了。

 

 

 

 

 

 

以上是关于objective-c中的深浅拷贝的主要内容,如果未能解决你的问题,请参考以下文章

深浅拷贝

关于:1.指针与对象;2.深浅拷贝(复制);3.可变与不可变对象;4.copy与mutableCopy的一些理解

C++中的深浅拷贝问题

python深浅拷贝

(转) 深浅拷贝问题

python学习之深浅拷贝