简体   繁体   中英

retain with objective-c

I have a question about a retain and a NSString, if I have a method who a return a NSString, and I put the return NSString in a nsstring variable, I must do a retain or not?

NSString *myString = @"";
myString = [self methodWhoReturnString]; // I must do this?
myString = [[self methodWhoReturnString]retain]; // Or I must do this?

The Apple Developer Documentation on Memory Management explains the scenarios where you retain/release objects.

Simply put, if you want the string to stick around, you need to retain it until you're finished with it. If that is just the scope of the current function, you can get away without retaining it as if the string is already autorelease'd (likely) it won't get released until your function finishes and the current AutoReleasePool is purged.

Bear in mind that an NSString * could actually be pointing to an NSMutableString *. If it matters to you if the string is changed by some other function without you realizing, be sure to copy it: NSString * myCopyOfString = [mystring copy];

If the string is set to autorelease, which it most likely is, then yes you will need to retain it somehow. I would suggest doing this though:

myString = [[self methodWhoReturnString] copy];

this ensures you have retained the data in the string not just a reference to a string that might still be controlled elsewhere. Be sure you release your copy later!

Any method that has alloc , new or copy in it automatically retains and infers that you have ownership of the object. All others shouldn't. It would be helpful if you had more context though. If we are in a contained method where this string is used briefly, then you probably don't need to retain. If it is going to be used for a while, you might want to use the @synthesize syntax to make it a property of the class you are in. When you use @property and @synthesize and call something like self.myProperty = something it will automatically retain.

Usually, methodWhoReturnString would return an autoreleased string, which means you should retain it if you want to keep it around.

So, if methodWhoReturnString is your method, I believe that to keep with convention you should return [stringToReturn autorelease]; from that method, and then retain it if you want to keep it.

You use retain if you're going to be using myString at a later point in time (ie after the current method has returned) to prevent it being autoreleased.

You don't need to use retain if it's just a temporary variable used within the current method, since in that case you do want it to be autoreleased.

One special case is properties. If you call self.blah = foo , you don't need to retain foo, since the setBlah: method should do it for you.

(there's a whole load of other complexities and edge cases, but this is a good rule of thumb to get you started on understanding what to do)

Given the code you provided, you shouldn't call -retain . In your example, the return value of a method that returns an instance of NSString is assigned to myString , an automatic local variable. If the assignment had been made to an instance variable or a static variable, you would want to call either retain or copy to guarantee that the reference remains valid beyond the end of the local scope.

In this case though, the reference to the NSString instance is stored in a variable that will be destroyed automatically at the end of the local scope, so your code needn't concern itself with the object's lifetime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM