简体   繁体   English

NSArray是否复制对象?

[英]Does NSArray copy objects?

When I make an NSArray using + [NSArray arrayWithObjects:] , does it copy those objects? 当我使用+ [NSArray arrayWithObjects:]制作NSArray ,它是否会复制这些对象? 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: 一如既往, 文档非常清楚地说明了这一点:

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. 数组保持对其内容的强引用 - 在托管内存环境中,每个对象在将id添加到数组之前接收保留消息,并在从数组中删除数据或者释放数组时接收释放消息。 If you want a collection with different object ownership semantics, consider using CFArray Reference, NSPointerArray, or NSHashTable instead. 如果您想要具有不同对象所有权语义的集合,请考虑使用CFArray Reference,NSPointerArray或NSHashTable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM