美文网首页
copy、strong、assign、weak修饰

copy、strong、assign、weak修饰

作者: xiaopaopao | 来源:发表于2022-06-27 14:48 被阅读0次

    我们在创建属性时,经常使用copy、strong、assign、weak等修饰词,使用不同的修饰词,属性会存在怎样的不同呢,这篇文章先不讲ios底层的原理,先概述一下现象级的结果;我们通过对NSString类型的属性,进行分析。

    copy

    1、NSString属性,NSString类型对象进行赋值

    <pre data-language="objectivec" id="svw5e" class="ne-codeblock language-objectivec" style="border: 1px solid rgb(232, 232, 232); border-radius: 2px; background-color: rgb(249, 249, 249); padding: 16px; font-size: 13px; color: rgb(89, 89, 89);">@property (nonatomic, copy) NSString *copString;

    NSString *string1 = @"1";
    self.copString = string1;
    NSLog(@"string1 = %p string1地址 =%p", string1, &string1);
    NSLog(@"copString = %p copString =%p", self.copString, &self->_copString);

    // 以下为打印的log
    string1 = 0x100004058 string1地址 =0x7ffeefbff3c8
    copString = 0x100004058 copString地址 =0x103205930</pre>

    2、NSString属性,NSMutableString类型对象进行赋值

    <pre data-language="objectivec" id="8Qd8i" class="ne-codeblock language-objectivec" style="border: 1px solid rgb(232, 232, 232); border-radius: 2px; background-color: rgb(249, 249, 249); padding: 16px; font-size: 13px; color: rgb(89, 89, 89);">@property (nonatomic, copy) NSString *copString;

    NSMutableString *string2 = [NSMutableString stringWithString:@"1"];
    self.copString = string2;
    NSLog(@"string2 = %p string2地址 =%p", string2, &string2);
    NSLog(@"copString = %p copString =%p", self.copString, &self->_copString);

    // 以下为打印的log
    string2 = 0x10286bc60 string2地址 =0x7ffeefbff3c0
    copString = 0xd5cb6b43590ee087 copString地址 =0x10286c0c0 // 这里的copString进行isa 64优化</pre>

    通过上述发现,当属性使用copy修饰时,对不可变对象赋值时,没有开辟新的内存,仅仅copy了一份地址,该内存的引用计数+1;对可变对象赋值时,开辟了新的内存。这种现象与copy与mutableCopy操作结果一致;

    3、NSMutableString属性,NSString类型对象进行赋值

    平时有些代码习惯的问题,修饰词使用错误,当属性为可变对象时使用了copy,又会是什么现象呢,我们创建一个可变字符串属性,使用copy修饰,然后进行上面的逻辑操作:

    <pre data-language="objectivec" id="DcLM0" class="ne-codeblock language-objectivec" style="border: 1px solid rgb(232, 232, 232); border-radius: 2px; background-color: rgb(249, 249, 249); padding: 16px; font-size: 13px; color: rgb(89, 89, 89);">@property (nonatomic, copy) NSMutableString *copString;

    NSString *string1 = @"1";
    self.copString = string1; // 这里会报警告 Incompatible pointer types assigning to 'NSMutableString *' from 'NSString *'
    NSLog(@"string1 = %p string1地址 =%p", string1, &string1);
    NSLog(@"copString = %p copString =%p", self.copString, &self->_copString);

    string1 = 0x100004058 string1地址 =0x7ffeefbff3c8
    copString = 0x100004058 copString地址 =0x103204220
    </pre>

    可变对象使用copy修饰,与不可变对象使用copy修饰的结果一样;但是这样就存在一种问题,可变对象指向了一块不可修改的内存区域,当修改可变对象的时候,会造成crash, // **Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with *******

    4、NSMutableString属性,NSMutableString类型对象进行赋值

    <pre data-language="objectivec" id="saGNy" class="ne-codeblock language-objectivec" style="border: 1px solid rgb(232, 232, 232); border-radius: 2px; background-color: rgb(249, 249, 249); padding: 16px; font-size: 13px; color: rgb(89, 89, 89);">@property (nonatomic, copy) NSMutableString *copString;

    NSMutableString *string2 = [NSMutableString stringWithString:@"1"];
    self.copString = string2;

    NSLog(@"string2 = %p string2地址 =%p", string2, &string2);
    NSLog(@"copString = %p copString地址 =%p", self.copString, &self->_copString);

    // log
    string2 = 0x100604b00 string2地址 =0x7ffeefbff3c0
    copString = 0xabc30a0efd4ed479 copString地址 =0x100604710</pre>

    可变对象使用copy修饰,对可变对象进行赋值,会开辟新的内存;不建议这样写,NSString属于最小单位对象,可变字符串赋值给copy修饰的可变属性,不会引起崩溃;但容器对象NSMutableArray、NSMutableDictionary等可变对象不能使用copy属性修饰,要使用strong;

    strong

    <pre data-language="objectivec" id="tqECQ" class="ne-codeblock language-objectivec" style="border: 1px solid rgb(232, 232, 232); border-radius: 2px; background-color: rgb(249, 249, 249); padding: 16px; font-size: 13px; color: rgb(89, 89, 89);">@property (nonatomic, strong) NSString *strongString;
    @property (nonatomic, strong) NSString *strongMutableString;

    NSString *string1 = @"1";
    self.strongString = string1;
    self.strongMutableString = string1;
    NSLog(@"string1 = %p string1地址 =%p", string1, &string1);
    NSLog(@"strongString = %p strongString地址 =%p", self.strongString, &self->_strongString);
    NSLog(@"strongMutableString = %p strongMutableString地址 =%p", self.strongMutableString, &self->_strongMutableString);

    NSMutableString *string2 = [NSMutableString stringWithString:@"1"];
    self.strongString = string2;
    self.strongMutableString = string2;
    NSLog(@"string2 = %p string2地址 =%p", string2, &string2);
    NSLog(@"strongString = %p strongString地址 =%p", self.strongString, &self->_strongString);
    NSLog(@"strongMutableString = %p strongMutableString地址 =%p", self.strongMutableString, &self->_strongMutableString);

    // log
    string1 = 0x100008058 string1地址 =0x7ffeefbff3c8
    strongString = 0x100008058 strongString地址 =0x1032041f8
    strongMutableString = 0x100008058 strongMutableString地址 =0x103204200

    string2 = 0x1004104c0 string2地址 =0x7ffeefbff3c0
    strongString = 0x1004104c0 strongString地址 =0x1032041f8
    strongMutableString = 0x1004104c0 strongMutableString地址 =0x103204200</pre>

    通过log发现,对象(可变对象、不可变对象)对使用strong属性修饰的对象(可变对象、不可变对象)赋值的时候,都不会开辟新的内存空间,指向同一块内存空间。当可变对象、不可变对象指针都指向了不可变内存,操作可变对象指针修改内存内容,会发生崩溃(提示找不到实现函数等待);当可变对象、不可变对象指针都指向了可变内存,操作可变对象指针修改内存内容,会引起内存数据发生变化,访问不可变对象值也发生变化;

    assign

    assgin修饰基本数据类型和基本数据对象。基础数据类型是分配在栈内,有系统分配和释放,所以不会造成野指针。

    修饰对象:如果用assign修饰对象,当对象被释放后,指针的地址还是存在的,也就是说指针并没有被置为nil,从而造成了野指针。因为对象是分配在堆上的,堆上的内存由程序员分配释放。而因为指针没有被置为nil,如果后续的内存分配中,刚好分配到了这块内存,就会造成崩溃。

    特殊情况:当assign属性修饰NSString对象时,如果使用语法糖的形式赋值,则字符串对象内存分配在常量区,常量区内存是不会被释放的,所以不会造成野指针崩溃;或者字符串对象的值小于64位,由于ios底层isa指针的优化,会将值小于64位的字符串值存储在isa指针中,属于taggedPoint类型字符串,该内存存储在栈内,也不会造成野指针崩溃情况。

    weak

    weak属于弱引用修饰词,不会对内存造成引用计数+1的情况,当内存释放时,该weak修饰的对象指针也会被设为nil,可参考:https://blog.csdn.net/weixin_38252066/article/details/107467593

    相关文章

      网友评论

          本文标题:copy、strong、assign、weak修饰

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