gpt4 book ai didi

iphone - removeObject 是否释放对象的 NSMutableArray 中的对象?

转载 作者:太空狗 更新时间:2023-10-30 03:34:47 26 4
gpt4 key购买 nike

我想知道当您在数组中使用 removeObject 删除对象时是否正确处理了删除的对象。被移除的对象会被释放吗?

最佳答案

NSMutableArray 会释放它。如果这是最后一次保留,它将被释放。来自文档:

Like NSArray, instances of NSMutableArray maintain strong references to their contents. If you do not use garbage collection [Jed: the iPhone does not], when you add an object to an array, the object receives a retain message. When an object is removed from a mutable array, it receives a release message. If there are no further references to the object, this means that the object is deallocated. If your program keeps a reference to such an object, the reference will become invalid unless you send the object a retain message before it’s removed from the array.

参见 NSMutableArray documentation .他们的例子,其实是指removeObjectAtIndex::

id anObject = [[anArray objectAtIndex:0] retain];
[anArray removeObjectAtIndex:0];
[anObject someMessage];

关于iphone - removeObject 是否释放对象的 NSMutableArray 中的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1668785/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com