开发者

Is stringByTrimmingCharactersInSet: an autoreleased string?

开发者 https://www.devze.com 2022-12-28 02:49 出处:网络
I can use the following codes to trim a string: -(void) aMethod { // myStr from a text box NSString *trimedStr = [mystr stringByTrimmingCharactersInSet:

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开发者_StackOverflow string? How can I find out that?


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.


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

0

精彩评论

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