美文网首页
iOS strong vs copy

iOS strong vs copy

作者: Gunks | 来源:发表于2018-04-14 12:16 被阅读0次
@property (nonatomic, strong) NSString *myStrongStr;
@property (nonatomic, copy) NSString *myCopyStr;

1.原对象不可变时,两种情况下都只是指针拷贝,计数+1。

    NSString *originStr = @"11";
    self.myCopyStr = originStr;
    self.myStrongStr = originStr;
    NSLog(@"originStr: %p", originStr);
    NSLog(@"myCopyStr: %p", self.myCopyStr);
    NSLog(@"myStrongStr: %p", self.myStrongStr);
2017-09-21 17:56:58.489 iOSLockDemo[40745:2801462] originStr: 0x10ba4a078
2017-09-21 17:56:58.490 iOSLockDemo[40745:2801462] myCopyStr: 0x10ba4a078
2017-09-21 17:56:58.490 iOSLockDemo[40745:2801462] myStrongStr: 0x10ba4a078

2.原对象可变时,strong修饰的对象,是指针拷贝,引用计数+1;copy修饰的对象是内存拷贝,生成新对象。

NSMutableString *originStr = [@"11" mutableCopy];
   self.myCopyStr = originStr;
   self.myStrongStr = originStr;
   NSLog(@"originStr: %p", originStr);
   NSLog(@"myCopyStr: %p", self.myCopyStr);
   NSLog(@"myStrongStr: %p", self.myStrongStr);
2017-09-21 18:04:44.904 iOSLockDemo[40919:2812754] originStr: 0x60000006d780
2017-09-21 18:04:44.904 iOSLockDemo[40919:2812754] myCopyStr: 0xa000000000031312
2017-09-21 18:04:44.905 iOSLockDemo[40919:2812754] myStrongStr: 0x60000006d780

本文参考链接:https://www.jianshu.com/p/d298606cae1e

相关文章

网友评论

      本文标题:iOS strong vs copy

      本文链接:https://www.haomeiwen.com/subject/glokkftx.html