When is NSCopying needed?

余生长醉 提交于 2019-12-05 14:57:55

问题


I know it's needed if your object will be used as a key in an NSDictionary. Are there any other times like this that NSCopying is required?

If I think I don't need my model objects to conform to NSCopying, am I probably wrong?


回答1:


When it's being passed to a copy property or any other method that is documented as copying its argument.




回答2:


Think of the NSCopying protocol as the objective-C version of cloning routines. If a caller was to clone your object, what is the behavior you would want? If your object is solely composed of primitive types, then you don't need to worry about this. But if you have any complex types as members, you might want to clone/copy them instead of passing simple references to them.




回答3:


NSCopying has quite broad usage. Anytime an object owns something it shouldn't share with other objects, you'll need a copy. Mutable objects often need to be copied, though NSMutableCopying might be needed instead of NSCopying.



来源:https://stackoverflow.com/questions/2573330/when-is-nscopying-needed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!