关于 iPhone 对象 C 数组以及深度和指针复制的基本问题

发布于 2024-11-01 18:33:57 字数 314 浏览 10 评论 0原文

我是 iPhone / Mac 领域的新手,这可能是一个非常基本的问题,我做了一些搜索,但没有找到直接的答案。

我想知道数组/可变数组的 addObject 方法在添加时是否执行浅(仅指针)或深(复制对象)。

  1. 已分配的可变数组
  2. 已通过某种 init 分配的 NSString
  3. 我们将字符串添加到可变数组中
  4. 然后释放 NSString

如果这是正确的处理方式,则假定 addObject 会执行此操作NSString 的深层副本。只是确认这是使用可变数组进行内存管理的正确方法......

I am new to the iPhone / Mac space and this is probably a pretty basic question, I have done some searching and have not found the direct answer.

I would like to know if the addObject method for Arrays / Mutable Arrays does a shallow (pointer only) or deep (copies object) when adding.

  1. A mutable array that has been alloc
  2. A NSString that has been alloc with some sort of init
  3. We addObject the string to the mutable array
  4. We then release the NSString

If this is the proper way to do things, that it is assumed that the addObject will do a deep copy of the NSString. Just confirming that this is the proper way to do memory management with mutable array ...

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

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

发布评论

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

评论(2

野鹿林 2024-11-08 18:33:57

addObject: 进行浅复制。您缺少的是 NSMutableArray 保留该对象,因此它不会消失,但不需要深层复制。这是 Objective-C 内存管理的一个关键点。您应该阅读内存管理编程指南 。 Cocoa 内存管理并不复杂,而且非常一致。我在三个魔词上有一个简短的摘要。

addObject: does a shallow copy. What you're missing is that NSMutableArray retains the object, so it does not go away, but does not require a deep copy. This is a key point to Objective-C memory management. You should read the Memory Management Programming Guide. Cocoa memory management is not complicated, and is incredibly consistent. I have a shortened summary at Three Magic Words.

温柔戏命师 2024-11-08 18:33:57

当将对象添加到数组时,其保留计数将会增加。当一个对象的保留计数为0时,它将被释放。因此,当将 NSString 添加到您的 anArray 时,它不会是一个副本(您可以更改 foo,它也会在数组中更改),但它会增加 foo 的保留计数1.

NSString *foo = [[NSString alloc] initWithString@"bar"]; // retain count +1
[anArray addObject:foo]; // NOT COPIED, but retain count of 'foo' increased by 1
[foo release]; // retain count of foo decreased by 1

当你在某个时候释放一个Array时,它会对其对象调用release。所以foo的retain count最终会变成0并且foo会被释放。

// some time later..
[anArray release]; // anArray released, foo retain count = 0.. foo gets released.

亲切的问候,
基督教

When adding an Object to an Array, its retain count will be increased. A object will be released when its retain count is 0. So when adding the NSString to you anArray it won't be a copy (you can change foo and it will be changed in the array, too) but it will increase foo's retain count by 1.

NSString *foo = [[NSString alloc] initWithString@"bar"]; // retain count +1
[anArray addObject:foo]; // NOT COPIED, but retain count of 'foo' increased by 1
[foo release]; // retain count of foo decreased by 1

When you at some time release anArray, it will call release on its objects. So foo's retain count will finally become 0 and foo will be released.

// some time later..
[anArray release]; // anArray released, foo retain count = 0.. foo gets released.

Kind Regards,
Christian

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