Title: retain with objective-c
Tags: iphone;objective-c
Question: 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?
Comment: Additionally to what's in the answers, you'll only need to retain/copy it, if you are actually storing it for later use. If you just use it immediately then even an autoreleased object is fine.

Here is the accepted answer: 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];

Here is another answer: 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.
Comment for this answer: This is wrong.  You own the object if you create it with `alloc` or `new`, or if you obtain it with a method containing `copy`, or you `retain` it.  `init` does nothing to the retain count. I would have down voted but the stuff about creating a property is good advice.
Comment for this answer: not that tough, nobody has down voted.  In fact, if you edit your answer to incorporate my comments and jlehr, I'd be tempted to up vote.
Comment for this answer: It's also incorrect to say that `self.myProperty = something` will automatically call `-retain`. That's only true if the property is declared with `retain` rather than -copy` or `assign` as its memory management semantic.
Comment for this answer: @JeremyP you are right. It is alloc, new* and *copy. I mixed them up. Sorry.

Here is another answer: You use retain if you're going to be using myString at a later point in time (i.e. 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)

Here is another answer: 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.

Here is another answer: 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.

Here is another answer: 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!

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 6, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2010:00:11/17/10', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<iphone><objective-c>', 'Title': 'retain with objective-c', 'ViewCount': 200, 'id': 4206005, 'user_AboutMe': None, 'user_AccountId': 636879.0, 'user_DisplayName': 'alex', 'user_Reputation': 871.0, 'user_Views': 36.0, 'user_id': 400380.0}}