考虑到内存管理,从 NSArray 获取 NSString 对象的正确方法是什么?

发布于 2024-11-07 09:25:49 字数 473 浏览 0 评论 0原文

从 NSArray 获取 NSString 对象的正确方法是什么,牢记内存管理。

假设我有一个数组

NSArray *myNewArray = [[NSArray alloc] initWithObjects:.......];

现在我想从索引 2 处的 NSArray 获取一个对象。

NSString *nameString = [myNewArray objectAtIndex:2]; // is it the right way? how to deal with "nameString" 
                                                     // now regarding memory managment, should I release it ?

或者我应该首先分配 nameString 然后为其赋值?

What is the right way to get an NSString object from an NSArray, keeping Memory Managment in mind.

Suppose I have an array

NSArray *myNewArray = [[NSArray alloc] initWithObjects:.......];

Now I want to get an object from this NSArray at index 2.

NSString *nameString = [myNewArray objectAtIndex:2]; // is it the right way? how to deal with "nameString" 
                                                     // now regarding memory managment, should I release it ?

OR I should first alloc nameString and then assign value to it ?

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

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

发布评论

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

评论(6

死开点丶别碍眼 2024-11-14 09:25:49

查看 Cocoa 内存管理规则(我用这句话开始发帖有多少次了?)。尤其

  • 您只能释放或自动释放您拥有的对象。
    • 如果您使用名称以“alloc”、“new”、“copy”或“mutableCopy”开头的方法(例如,alloc、newObject 或 mutableCopy)创建对象,或者如果您向其发送一条保留消息。
    • 您可以使用release或autorelease来放弃对象的所有权。 autorelease 只是意味着“将来发送释放消息”(具体来说:当使用的自动释放池收到耗尽消息时 - 要了解何时发生,请参阅“自动释放池”)。

方法 objectAtIndex: 是否以“alloc”开头?不是,是“new”开头的吗?不。它以“copy”还是“mutableCopy”开头?否。您是否已将 retain 发送到返回的对象?不。

因此您不拥有 nameString。因此您不得释放或自动释放它。

抱歉,如果上面的内容看起来有点“被牵着鼻子走”,但是当我第一次开始使用 Objective-C 时,我发现以这种方式在我的脑海中仔细浏览以上所有内容是很有用的,否则我往往会弄错。没过多久,这一切就会成为第二天性。

Check out the Cocoa Memory Management rules (how many times have I started a post with that sentence?). In particular

  • You only release or autorelease objects you own.
    • You take ownership of an object if you create it using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message.
    • You use release or autorelease to relinquish ownership of an object. autorelease just means “send a release message in the future” (specifically: when the used autorelease pool receives a drain message—to understand when this will be, see “Autorelease Pools”).

Does the method objectAtIndex: begin with "alloc"? No. Does it begin with "new"? No. Does it begin with "copy" or "mutableCopy"? No. Have you sent retain to the returned object? No.

Therefore you do not own nameString. Therefore you must not release or auto release it.

Sorry if the above seems a bit "leading by the nose" but when I first started with Objective-C, I found it useful to pretty much go through all the above in my head in exactly that way, otherwise I tended to get it wrong. It doesn't take long for it all to become second nature.

我很坚强 2024-11-14 09:25:49

我认为您还没有完全理解“指针”的概念。您的变量 nameString 只是一个指针。不是字符串。

在该行中:

NSString *nameString = [myNewArray objectAtIndex:2];

您只需将指针分配给数组第二个对象的实际内存地址。就这样。

如果您要保持此对象处于活动状态(即使数组将被释放),您最好保留该对象。

I think you don't totally get the "pointer" concept yet. Your variable nameString is just a pointer. Not a string.

In the line :

NSString *nameString = [myNewArray objectAtIndex:2];

You just assign the pointer to the actual memory address of the second object of the array. That's all.

If you are about to keep this object alive (even if the array will be deallocted), you better retain that object.

勿忘心安 2024-11-14 09:25:49
NSArray *myNewArray = [[NSArray alloc] initWithObjects:.......];
NSString *nameString = [myNewArray objectAtIndex:2]
[myArray release];

就这样吧,没必要再费心了。 NSString 将由环境本身释放。

NSArray *myNewArray = [[NSArray alloc] initWithObjects:.......];
NSString *nameString = [myNewArray objectAtIndex:2]
[myArray release];

That's it, no need to bother any more with it. NSString will be released by environment itself.

幽梦紫曦~ 2024-11-14 09:25:49

不需要分配或初始化 NSString 对象,毕竟如果你没有分配任何内存,那么就不需要释放......只释放 NSArray 对象,没有别的......

你正在经历正确的方式......

There is no need to allocate or initialize NSString object and after all if you are not allocating any memory then there is no need to release....Only release NSArray objects nothing else....

You are going through right way...

兔姬 2024-11-14 09:25:49

不需要释放 nameString。只有在使用(alloc”, “new”, “copy”, or “mutableCopy”) 时才释放。

There is no need to release the nameString.You only release when you are using(alloc”, “new”, “copy”, or “mutableCopy”) .

半﹌身腐败 2024-11-14 09:25:49

NSArray *myNewArray = [[NSArray alloc] initWithObjects:.......];
从任何 index 获取 NSString 中的值都不会影响内存...无需在此处分配。
收集到的 NSString 不需要保留,直到您将其所有权传递给另一个控制器/对象。

NSArray *myNewArray = [[NSArray alloc] initWithObjects:.......];
and getting value in NSString from any of index wont affect memory ...no need to allocate here.
The collected NSString need not required to be retained until you are passing its ownership to another controller/object.

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