This is from the memory management programming guide:
Technique 1 In technique 1, the values returned by the getter are automatically implemented within the scope of the call:
- (NSString*) title { return [[title retain] autorelease]; } - (void) setTitle: (NSString*) newTitle { if (title != newTitle) { [title release]; title = [newTitle retain];
Since the object returned from get accessor is auto-implemented in the current scope, it remains valid if the property value changes. This makes the accessory more reliable, but at the cost of additional overhead. If you expect your getter method to be called frequently, the added cost of saving and auto-implementing the object may not be worth the cost of performance.
Technique 2 Like method 1, method 2 also uses the automatic estimation method, but this time does it in the setter method:
- (NSString*) title { return title; } - (void) setTitle: (NSString*) newTitle { [title autorelease]; title = [newTitle retain];
The effectiveness of technique 2 is much better than technique 1 in situations where the getter is called much more often than the installer.
Technique 3 Technique 3 completely excludes the use of abstract:
- (NSString*) title { return title; } - (void) setTitle: (NSString*) newTitle { if (newTitle != title) { [title release]; title = [newTitle retain];
The approach used by technique 3 is good for the often called getter and setter methods. It is also good for objects that do not want to extend the life of their values, such as collection classes. Its disadvantage is that the old value can be immediately released (if there are no other owners), which will cause a problem if another object supports a link that is not related to it. For instance:
NSString *oldTitle = [anObject title]; [anObject setTitle:@"New Title"]; NSLog(@"Old title was: %@", oldTitle);
If anObject was the only object that owned the original header line, the line will be freed after setting a new header. The log statement then fails because oldTitle is a free object.
EDIT: Basically, the save point and then auto-release is to ensure that the object will not be freed if the property value is changed before the call area can save it. This is usually not a problem unless you have asynchronous code. In most situations - (Weapon *)myWeapon { return myWeapon; } - (Weapon *)myWeapon { return myWeapon; } just fine (plus it's faster).