首页 > 学院 > 开发设计 > 正文

objective-c中的深、浅拷贝

2019-11-14 19:20:09
字体:
来源:转载
供稿:网友

浅复制:只复制指向对象的指针,而不复制引用对象本身。计数器+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") 0x1001145702015-04-01 20:46:23.133 04-01-person[2204:165655] array2:(    "two day") 0x1001147002015-04-01 20:46:23.133 04-01-person[2204:165655] array3:(    "two day") 0x1001147002015-04-01 20:46:23.133 04-01-person[2204:165655] array4:(    "two day") 0x1001147002015-04-01 20:46:23.133 04-01-person[2204:165655] array5:(    "two day") 0x1001145702015-04-01 20:46:23.133 04-01-person[2204:165655] array6:(    "two day") 0x1001150d02015-04-01 20:46:23.134 04-01-person[2204:165655] array7:(    "two day") 0x100115b002015-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)  0x100106c802015-04-01 21:00:28.220 04-01-person[2252:170414] array02: (    zhang,    wang,    li)  0x1001076d0Program 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)  0x1001129002015-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)  0x1001145702015-04-01 20:57:41.734 04-01-person[2243:169571] array2: (    zhang,    wang,    li)  0x100115020Program 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)  0x1006069a02015-04-01 20:53:07.399 04-01-person[2217:167996] array2: (    one,    two,    three)  0x1006069a0Program ended with exit code: 0
运行结果

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

 

 

 

 

 

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表