Does NSArray copy objects?

a 夏天 提交于 2019-11-27 03:52:06

问题


When I make an NSArray using +[NSArray arrayWithObjects:], does it copy those objects? If I release the objects after adding them to the array, will I run into problems?


回答1:


No, it doesn't copy them. It retains them. Yes, you can safely release the objects after adding them to the array.

The docs, as always, spell this out very clearly:

Arrays maintain strong references to their contents—in a managed memory environment, each object receives a retain message before its id is added to the array and a release message when it is removed from the array or when the array is deallocated. If you want a collection with different object ownership semantics, consider using CFArray Reference, NSPointerArray, or NSHashTable instead.



来源:https://stackoverflow.com/questions/1258735/does-nsarray-copy-objects

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