Objective-C中的Strong、Copy与MutableCopy
面试过程中经常被问到ARC中Strong、Copy的区别是什么。普通的回答是:一样。文艺(正确)的回答是:分情况(我擦!WQY#$&Y**%OWEUR)
可以先参考这篇文章http://www.cnblogs.com/lihaiyin/p/4647426.html
问题一:到底用Copy还是Strong
1. 把不可变对象写成Copy: 如果把不可变对象赋值给此属性,内存中其实就是retain了一下。 如果把可变对象赋值给此属性,会生成新的不可变对象,避免值的变化
2. 把不可变对象写成Strong: 如果把不可变对象赋值给此属性,内存中其实就是retain了一下。 如果把可变对象赋值给此属性,会导致赋值后的内容依然可变
3. 把可变对象写成Strong: 如果把不可变对象赋值给此属性,调用此对象的addXX等增、删的方法时会崩溃。如果把可变对象赋值给此属性,内存中其实就是retain了一下。
4. 把可变对象写成Copy: 无论是谁赋值给此属性,都会调用Copy生成不可变对象,都会在调用此对象的addXX等增、删的方法时会崩溃
因此,我们的代码中一般都会 把不可变对象写成Copy 把可变对象写成Strong
问题二:源码中的Copy方法为什么copyItem的参数是YES?
/** * Returns a new copy of the receiver.<br /> * The default abstract implementation of a copy is to use the * -initWithArray:copyItems: method with the flag set to YES.<br /> * Immutable subclasses generally simply retain and return the receiver. */ - (id) copyWithZone: (NSZone*)zone { NSArray *copy = [NSArrayClass allocWithZone: zone]; return [copy initWithArray: self copyItems: YES]; } /** * Returns an NSMutableArray instance containing the same objects as * the receiver.<br /> * The default implementation does this by calling the * -initWithArray:copyItems: method on a newly created object, * and passing it NO to tell it just to retain the items. */ - (id) mutableCopyWithZone: (NSZone*)zone { NSMutableArray *copy = [NSMutableArrayClass allocWithZone: zone]; return [copy initWithArray: self copyItems: NO]; }
对比一下,发现initWithArray:copyItems方法的参数很怪。都知道,无论NSArray是copy还是mutableCopy,item都是retain的,不会copy,那为啥copyWithZone方法(copy方法调用时会调用此方法)的实现中,copyItem的参数是YES?
-initWithArray:copyItems: method with the flag set to YES.<br /> * Immutable subclasses generally simply retain and return the receiver
想了很久,后来发现,答案就在注释中,不可变子类(NSString、NSArray、NSDictionary)一般只是简单的retain。。。对不可变对象调用copy只是retain,对可变对象调用copy会生成不可变对象,所以,为了使得新生成的NSArray“不可变”(元素本身也要不可变),只能使用copy,但这里的copy并非为item生成了新的对象,只是为了生成“不可变”的对象
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库