NSMutableArray 释放索引处的对象
我按以下方式创建 NSMUtableArray:
self.myArray = [NSMutableArray array];
for (int i = 0; i < number; i++) {
[self.myArray addObject:[NSNull null]];
}
然后,我将一个对象添加到给定索引处的数组中,如下所示:
[self.myArray replaceObjectAtIndex:myIndex withObject:myObj];
因为 myArray 包含相当大尺寸的对象(图像),所以我希望能够通过释放未使用的对象(myArray保存要在 UIScrollView 中使用的图像数据)。
在给定索引处释放 NSMutableArray 成员的正确方法是什么?我正在考虑使用:
[self.myArray replaceObjectAtIndex:myIndex withObject:[NSNull null]];
I create my NSMUtableArray the following way:
self.myArray = [NSMutableArray array];
for (int i = 0; i < number; i++) {
[self.myArray addObject:[NSNull null]];
}
I then add an object to the array at a given index like this:
[self.myArray replaceObjectAtIndex:myIndex withObject:myObj];
Because myArray contains objects of rather large size (images), I want to be able to release the memory by releasing unused objects (myArray holds the data of images to be used in a UIScrollView).
What would be the correct way to release a member of NSMutableArray at a given index? I was thinking of using:
[self.myArray replaceObjectAtIndex:myIndex withObject:[NSNull null]];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您替换对象时,数组将释放当前位于该索引处的任何对象。因此,如果您将大对象替换为 [NSNull null] 之类的内容,那么它应该可以满足您的要求。
请注意,除非保留计数变为 0,否则释放的对象的内存实际上不会被释放。如果存在对该对象的其他引用,即代码中保留它的任何其他位置,那么它将保留大约。如果数组是唯一保留对象的东西,那么你应该可以开始了。
The array will release whatever object is currently at that index when you replace the object. So if you replace your big object with something like [NSNull null] that should do what you want.
Note that the memory for the object that as released won't actually be freed unless the retain count goes to 0. If there are other references to the object i.e. any other place in the code where you've retained it, then it will stick around. If the array is the only thing retaining the object, you should be good to go.