1 or 2 ??? I REALLY care about memory. Even the last byte...
The second one is correct.
stringWithFormat will return a new string, which is set to autorelease.
So in the first example, you first allocate a string and then you assign your string pointer to a new object, meaning the string you just allocated on the first line will never be released.
Hope it makes sense.
Besides the problem with the first code that kasparp pointed out, it also doesn't follow the naming convention in regards to returning owned or autoreleased objects.
Besides the problem with the first code that kasparp pointed out, it also doesn't follow the naming convention in regards to returning owned or autoreleased objects.
Thanks, really helpful guys.
One more thing.
Is this:
Code:
return @"value";
considered to be the same as this:
Code:
return [NSString stringWithString:@"value"];
???
Also,
When am i using a value like something that is helping me to figure out what i should return:
Depends on what "doSomething" returns, an owned or an autoreleased object. By naming convention it should be autoreleased in that case. You should really read the Memory Management Programming Guide, this is all explained there.
Oh, and for the first question, @"" are actually string constants, just treat them as if they were autoreleased objects, balance retains with release calls and you'll be fine.
Depends on what "doSomething" returns, an owned or an autoreleased object. By naming convention it should be autoreleased in that case. You should really read the Memory Management Programming Guide, this is all explained there.
Oh, and for the first question, @"" are actually string constants, just treat them as if they were autoreleased objects, balance retains with release calls and you'll be fine.
If you've read the Mem Management guide you would know. Is reading a short but essential guide that hard for you? You keep stumbling in the dark and coming back with these basic questions for months now, and you could have avoided it all with a days read.
first one is right. second one is wrong.
the static function "stringWithString..." returns an autorelease object. so no need to redeclare autorelease for it.
the only reason to use autorelease is when using alloc like so: