tags:

views:

148

answers:

2

I can use the following codes to trim a string:

-(void) aMethod {
// myStr from a text box
NSString *trimedStr = [mystr stringByTrimmingCharactersInSet:
  [NSCharacterSet whitespaceAndNewlineCharacterSet]];
...
// should I release trimedStr?
}

Not sure if the result trimedStr is an autorelease string? How can I find out that?

A: 

Yes, it is autoreleased.

Run, don't walk, to the Object Ownership Policy section of the Memory Management Programming Guide to find out how you can tell.

(In short, because the method name does not begin "alloc" or "new", or contain the word "copy", you don't own it, and don't need to release it.)

David Gelhar
+1  A: 

The fundamental rule of memory management in Objective-C

You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message. You are responsible for relinquishing ownership of objects you own using release or autorelease. Any other time you receive an object, you must not release it.

Since the name does not start with alloc or new and does not contain copy, it is autoreleased.

Brandon Bodnár