开发者

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

What is the right way to get an NSString object from an NSArray, 开发者_如何学C 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 ?


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.


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.


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.


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...


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:.......]; 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜