对象的深浅拷贝


#import "DSJCopyCtrl.h"

@interface DSJCopyCtrl ()

@end

@implementation DSJCopyCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"深浅拷贝";
    [self strCopy];
    //非容器类 不可变  mutablecopy 会产生新对象 ,变成可变
   // 非容器类 可变 copy mutablecopy 都产生新对象  ,其中 mutablecopy 产生的对象可变 ,copy 出来的对象变成不可变了
    [self arrCopy];
    [self dicCopy];
    [self setCopy];
    
    //容器类 不可变 mutablecopy 会产生新对象 ,变成可变
    //容器类 可变 copy mutablecopy 都会产生新对象 ,其中 mutablecopy 产生的对象可变 ,copy 出来的对象变成不可变了
    /*
     总结:
     1.浅拷贝是指针拷贝,不产生新对象。
     2.深拷贝是对象拷贝,开辟了新空间,产生新对象。
     3.copy 不一定是浅拷贝,对象进行copy后一定不可变。
     4.mutablecopy  一定是深拷贝 。
     5.可变类型进行拷贝一定产生新对象 。
     */
}


- (void)strCopy {
    NSString *str = @"123";//0x1004380c8 NSCFConstantString
    NSString *str2 = str.copy;//0x1004380c8 NSCFConstantString
    NSString *str3 = str.mutableCopy;//0x2832d2d60 NSCFString
    [((NSMutableString *)str3) appendString:@"456"];//不会奔溃
    NSLog(@"str :%p  str2:%p str3:%p",str,str2,str3);
    
    NSMutableString *str4 = @"123"; //0x1004380c8 NSCFConstantString
    NSMutableString *str5 = str4.copy;//0x1004380c8 NSCFConstantString
    NSMutableString *str6 = str4.mutableCopy;//0x2832d27c0 NSCFString

    NSLog(@"str4 :%p  str5:%p str6:%p",str4,str5,str6);
    
    NSMutableString *str7 = [NSMutableString stringWithString:@"123"]; //0x282131890  NSCFString
    NSMutableString *str8 = str7.copy;//0xffeaea764c242ed8 NSTaggedPointerString
    //FIXME: -    [str8 appendString:@"456"];//会奔溃
    /*
     *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString appendString:]: unrecognized selector sent to instance 0x819f31a542d50008'
     */
    NSMutableString *str9 = str7.mutableCopy;//0x282130240 NSCFString
    NSLog(@"str7 :%p  str8:%p str9:%p",str7,str8,str9);
}


- (void)arrCopy {
    
    NSArray *arr = @[@1,@2,@3];//0x281eee940 NSArryI
    NSArray *arr1 = arr.copy;//0x281eee940 NSArryI
    NSArray *arr2 = arr.mutableCopy;//0x281ec5e60 NSArryM
    NSLog(@"arr:%p arr1:%p arr2:%p",arr,arr1,arr2);

    NSMutableArray *arr3 = [NSMutableArray arrayWithObject:@"1"];//0x281eedb60 NSArryM
    NSMutableArray *arr4 = arr3.copy;//0x2812a4580 NSSingleObjectArrayIle
    //FIXME:- [arr4 addObject:@"2"];//会奔溃
    /*
     *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSingleObjectArrayI addObject:]: unrecognized selector sent to instance 0x281474550'
     */
    NSMutableArray *arr5 = arr3.mutableCopy;//0x281eef780 NSArryM
    [arr5 addObject:@"2"];
    NSLog(@"arr3:%p arr4:%p arr5:%p",arr3,arr4,arr5);
}

- (void)dicCopy {
    NSDictionary *dic = @{@"key":@"value"};//0x2810955e0 NSSingleEntryDictionaryI
    NSDictionary *dic1 = dic.copy;//0x2810955e0 NSSingleEntryDictionaryI
    NSDictionary *dic2 = dic.mutableCopy;//0x2810943e0 NSDictionaryM
    [((NSMutableDictionary *)dic2) setObject:@"dsj" forKey:@"name"];
    NSLog(@"dic:%p dic1:%p dic2:%p",dic,dic1,dic2);
    
    NSMutableDictionary *dic3 = [NSMutableDictionary dictionaryWithDictionary:@{@"key":@"value"}];// 0x28109b0c0 NSDictionaryM
    NSMutableDictionary *dic4 = dic3.copy; //0x28109bb80 NSFrozenDictionaryM
    //FIXME:- [dic4 setObject:@"dsj" forKey:@"name"];//会奔溃
    /*
     *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSFrozenDictionaryM setObject:forKey:]: unrecognized selector sent to instance 0x283f35ec0'
     */
    NSMutableDictionary *dic5 = dic3.mutableCopy;//0x28109ba80 NSDictionaryM
    NSLog(@"dic3:%p dic4:%p dic5:%p",dic3,dic4,dic5);
    
}

- (void)setCopy {
    NSSet *set = [[NSSet alloc] initWithArray:@[@1]];//0x2812aceb0 NSSingleObjectSetI
    NSSet *set1 = set.copy;//0x2812aceb0 NSSingleObjectSetI
    NSSet *set2 = set.mutableCopy;//0x28109b0c0 NSSetM
    NSLog(@"set:%p set1:%p set2:%p",set,set1,set2);
    
    NSMutableSet *set3 = [[NSMutableSet alloc] initWithArray:@[@1]];//0x281094c40 NSSetM
    NSMutableSet *set4 = set3.copy;//0x2812ac1d0 NSSingleObjectSetI
    //FIXME:- [set4 addObject:@2];//会奔溃
    /**** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSingleObjectSetI addObject:]: unrecognized selector sent to instance 0x282868b60'
     */
    NSMutableSet *set5 = set3.mutableCopy;//0x2810951a0 NSSetM
    NSLog(@"set3:%p set4:%p set5:%p",set3,set4,set5);
}
@end

结论:
1.浅拷贝是指针拷贝,不产生新对象。
2.深拷贝是对象拷贝,开辟了新空间,产生新对象。
3.copy 不一定是浅拷贝,对象进行copy后一定不可变。
4.mutablecopy 一定是深拷贝 。
5.可变类型进行拷贝一定产生新对象 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值