//可变集合类和不可变集合类的copy和mutablecopy有什么区别?如果是集合是内容复制的话,集合里面的元素也是内容复制么?
- (void)copyAndMutableCopy{
NSMutableArray *mutableAry = [NSMutableArray arrayWithObjects:@"a",nil];
NSArray *ary = [NSArray arrayWithObjects:@"b",nil];
id copyMutableAry = [mutableAry copy];
id copyAry = [ary copy];
id mutableArymutableCopy = [mutableAry mutableCopy];
id mutableCopyAry = [ary mutableCopy];
NSLog(@"mutableAry:%@ ,内存地址%p -- 指针地址%p",mutableAry,mutableAry,&mutableAry);
NSLog(@"ary:%@ ,内存地址%p -- 指针地址%p",ary,ary,&ary);
NSLog(@"==================这是华丽丽的分割线==================");
NSLog(@"copyMutableAry:%@ ,内存地址%p -- 指针地址%p",copyMutableAry,copyMutableAry,©MutableAry);
NSLog(@"copyAry:%@ ,内存地址%p -- 指针地址%p",copyAry,copyAry,©Ary);
NSLog(@"==================这是华丽丽的分割线==================");
NSLog(@"mutableArymutableCopy:%@ ,内存地址%p -- 指针地址%p",mutableArymutableCopy,mutableArymutableCopy,&mutableArymutableCopy);
NSLog(@"mutableCopyAry:%@ ,内存地址%p -- 指针地址%p",mutableCopyAry,mutableCopyAry,&mutableCopyAry);
}
log
2017-08-10 16:04:40.287 Test[13049:755504] mutableAry:(a) ,内存地址0x61000004ce40 -- 指针地址0x7fff54dbfa58
2017-08-10 16:04:40.287 Test[13049:755504] ary:(b) ,内存地址0x61000000ff50 -- 指针地址0x7fff54dbfa50
2017-08-10 16:04:40.288 Test[13049:755504] ==================这是华丽丽的分割线==================
2017-08-10 16:04:40.288 Test[13049:755504] copyMutableAry:(a) ,内存地址0x61000000ff60 -- 指针地址0x7fff54dbfa48
2017-08-10 16:04:40.288 Test[13049:755504] copyAry:(b) ,内存地址0x61000000ff50 -- 指针地址0x7fff54dbfa40
2017-08-10 16:04:40.288 Test[13049:755504] ==================这是华丽丽的分割线==================
2017-08-10 16:04:40.288 Test[13049:755504] mutableArymutableCopy:(a) ,内存地址0x61000004cd20 -- 指针地址0x7fff54dbfa38
2017-08-10 16:04:40.288 Test[13049:755504] mutableCopyAry:(b) ,内存地址0x61000004ce10 -- 指针地址0x7fff54dbfa30
总结:使用copy时 可变集合的指针地址以及内存地址都不相同 深复制 不可变集合的指针地址不一样但是内存地址一样 属于浅复制
使用mutableCopy的时候无论是可变集合还是不可变集合的指针地址和内存地址都不同 都属于深复制