随笔 - 91,  文章 - 0,  评论 - 4,  阅读 - 13万

 

一、先介绍 copy、strong、weak 的区别,如代码所示

复制代码
@property(copy,nonatomic)NSMutableString*aCopyMStr; 
@property(strong,nonatomic)NSMutableString*strongMStr; 
@property(weak,nonatomic)NSMutableString*weakMStr; 
@property(assign,nonatomic)NSMutableString*assignMStr; 

NSMutableString *mstrOrigin = [[NSMutableStringalloc] initWithString:@"mstrOriginValue"]; 

self.aCopyMStr= mstrOrigin; 
self.strongMStr= mstrOrigin; 
self.weakMStr= mstrOrigin; 
NSLog(@"mstrOrigin输出:%p,%@\\n", mstrOrigin,mstrOrigin); NSLog(@"aCopyMStr输出:%p,%@\\n",_aCopyMStr,_aCopyMStr); 
NSLog(@"strongMStr输出:%p,%@\\n",_strongMStr,_strongMStr); 
NSLog(@"weakMStr输出:%p,%@\\n",_weakMStr,_weakMStr); 
NSLog(@"引用计数%@",[mstrOriginvalueForKey:@"retainCount"]); //输出结果 

//2016-09-01 15:19:13.134 lbCopy[1205:87583] mstrOrigin输出:0x7892a5e0,mstrOriginValue 
//2016-09-01 15:19:13.135 lbCopy[1205:87583] aCopyMStr输出:0x7893deb0,mstrOriginValue 
//2016-09-01 15:19:13.135 lbCopy[1205:87583] strongMStr输出:0x7892a5e0,mstrOriginValue 
//2016-09-01 15:19:13.135 lbCopy[1205:87583] weakMStr输出:0x7892a5e0,mstrOriginValue 
//2016-09-01 15:19:13.135 lbCopy[1205:87583] 引用计数2
复制代码

 

结论:

1、copy 和 strong 引用计数器加一,weak 引用计数器不加一。

2、strong 和 weak 的内存地址都指向 mstrOrigin,copy 为创建新的内存地址并复制内容,再指向 mstrOrigin。

 

二、修改 mstrOrigin 的值的时候,必然不会影响aCopyMStr,只会影响strongMStr和weakMStr

三、将 mstrOrigin 置为 nil,strong 和 weak 都为 nil,copy 不为 nil

 

四、assign

assign 引用计数器不加一,但是要为指向的置为空时,只是进行值释放。这就导致野指针存在,即当这块地址还没写上其他值前,能输出正常值,但一旦重新写上数据,该指针随时可能没有值,造成奔溃。

 

posted on   xiao孛  阅读(188)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2016-05-17 ios app打ipa包

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示