Retain followed by autorelease does exactly what you might think it does. It sends the object a retain
message and then sends it an autorelease
message. Remember that autoreleasing an object adds that object to the autorelease pool but does not release it yet. The autorelease pool will send the object a release
message at the end of the current iteration of the run loop. So, a retain
followed by autorelease
essentially says, "Make sure this object stays around until the end of the the current iteration of the run loop." If you need the returned value to hang around longer, you can retain it. If not, do nothing and the autorelease pool will handle it.
In this case, the string being sent retain
and autorelease
messages is a property. It's already retained by the parent object. So you might wonder why do this retain and autorelease thing at all? Well, there's no guarantee that the object won't release _identifier
before the current iteration of the run loop ends. Consider this example:
- (NSString *)identifier { return _identifier; }
- (void)aMethod {
NSString *localId = [self identifier]; // localId refers to _identifier which is only retained by self
[self methodThatChangesIdentifierAndReleasesItsOldValue]; // self releases _identifier
NSLog(@"%@", localId); // crash, because localId (old value of _identifier) has been released
}
In this case, the returned identifier isn't retained and autoreleased. The NSLog
line crashes because localId
refers to a released string. However, had we used retain
and autorelease
in the getter, there would be no crash because localId
would not be released until the end of the current iteration of the run loop.
As far as I know, your replacement with an identifier
property would be just as good. It might not be identical code, but the effect should be the same.