NSArray是否复制对象? [英] Does NSArray copy objects?
问题描述
当我使用+ [NSArray arrayWithObjects:]
制作NSArray
时,它会复制那些对象吗?如果将对象添加到数组后释放它们,是否会遇到问题?
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?
推荐答案
否,它不会复制它们.它保留了它们.是的,您可以在将对象添加到数组后安全地释放它们.
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:
数组对它们的内容有很强的引用-在托管内存环境中,每个对象在将其ID添加到数组之前都会收到保留消息,并在从数组中删除或释放数组时收到释放消息.如果要使用具有不同对象所有权语义的集合,请考虑改用CFArray参考,NSPointerArray或NSHashTable.
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.
这篇关于NSArray是否复制对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!