考虑到内存管理,从 NSArray 获取 NSString 对象的正确方法是什么?
从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
查看 Cocoa 内存管理规则(我用这句话开始发帖有多少次了?)。尤其
方法
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
Does the method
objectAtIndex:
begin with "alloc"? No. Does it begin with "new"? No. Does it begin with "copy" or "mutableCopy"? No. Have you sentretain
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.
我认为您还没有完全理解“指针”的概念。您的变量 nameString 只是一个指针。不是字符串。
在该行中:
您只需将指针分配给数组第二个对象的实际内存地址。就这样。
如果您要保持此对象处于活动状态(即使数组将被释放),您最好保留该对象。
I think you don't totally get the "pointer" concept yet. Your variable nameString is just a pointer. Not a string.
In the line :
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.
就这样吧,没必要再费心了。 NSString 将由环境本身释放。
That's it, no need to bother any more with it. NSString will be released by environment itself.
不需要分配或初始化 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...
不需要释放 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”) .
NSArray *myNewArray = [[NSArray alloc] initWithObjects:.......];
从任何
index
获取NSString
中的值都不会影响内存...无需在此处分配。收集到的 NSString 不需要保留,直到您将其所有权传递给另一个控制器/对象。
NSArray *myNewArray = [[NSArray alloc] initWithObjects:.......];
and getting value in
NSString
from any ofindex
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.