The Apple Memory Management Programming Guide shows three officially sanctioned methods for writing access methods that should keep or free object references.
In the case of the first two methods (reproduced below), Apple's documentation says that "the effectiveness of Technique 2 is significantly better than Technique 1 in situations where the getter is called much more often than the setter."
// Technique 1 - (NSString*) title { return [[title retain] autorelease]; } - (void) setTitle: (NSString*) newTitle { if (title != newTitle) { [title release]; title = [newTitle retain]; // Or copy, depending on your needs. } } // Technique 2 - (NSString*) title { return title; } - (void) setTitle: (NSString*) newTitle { [title autorelease]; title = [newTitle retain]; // Or copy, depending on your needs. }
Is this the only difference between technique 1 and technique 2, or does using one over the other have other subtle consequences that I may need to know about? And if method 2 uses a more efficient getter, does it follow that method 1 uses a more efficient getter because title gets an explicit (and presumably immediate) release?
source share