从数组中删除对象

发布于 2024-09-14 23:38:03 字数 97 浏览 3 评论 0原文

我正在 Objective-C 上开发 iPhone 应用程序 我有带有对象引用的数组 如果我从数组中删除带有对象引用的项目,我是否应该另外释放该对象,或者它将自动从内存中删除?

i'm working on app for iphone on objective-c
i have array with object references
If i remove item with object reference from array should i release this object additionally or it will be removed from memory automatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无声静候 2024-09-21 23:38:03

当从数组中删除时,对象会被释放一次。因此,如果您的保留/释放在其他地方正确配对,那么在这种情况下您一定不能释放您的对象。

When removed from array object gets released once. So if your retain/release are paired correctly in other places you must not release your object in this case.

潜移默化 2024-09-21 23:38:03

如果 NSArray(嗯,它一个 NSArray,不是吗?C 数组不提供所有权管理)是唯一拥有的东西对象 - 也就是说,如果添加的对象是获取 autorelease-d 或者您在添加后显式调用 release - 那么它将在删除时自动清理。任何其他所有权声明仍需要像平常一样release-d。

If the NSArray (um, it is an NSArray, isn't it? C arrays provide no ownership management) is the only thing that owns the object -- that is, if the object added was acquired autorelease-d or you explicitly called release after adding -- then it will be cleaned up automatically on removal. Any other ownership claims will still need to be release-d as normal.

把回忆走一遍 2024-09-21 23:38:03

事实上,您谈论的 NSMutableArray

它会“自动”释放。
所以做

[array add:  @"SAFEY-STRING" ];

和不做

[array add:  [[NSString alloc] initWithFormat:@"LEAKY-STRING"] ];

In fact, your talking about NSMutableArray

And it does the release 'automatically'.
So do

[array add:  @"SAFEY-STRING" ];

and don't do

[array add:  [[NSString alloc] initWithFormat:@"LEAKY-STRING"] ];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文