Objective-C 2.0 Accessors & Memory Management
Quite often, you may have simple setter methods that need to do a just a tiny bit of work before or after setting an instance variable. For example, maybe you need to redraw something after setting the property of an object. So, instead of writing this:
[self setBackgroundColor:[NSColor blueColor]]; [view setBackgroundColor:[NSColor blueColor]];You’d probably want to move the relevant code to your -setBackgroundColor:
accessor instead:
Then you can simply call -setBackgroundColor:
and expect it all to work nicely:
(You could use Key-Value Observing to do this, but I generally avoid KVO for simple intra-class property dependencies like this. I don’t think the overhead of maintaining all the KVC dependencies and KVO-related methods is worth the cost.)
Of course, the above method requires that you write all that stupid boilerplate memory management code in the accessor. Instead of doing that, I tend to declare a private _backgroundColor
property in the class, @synthesize
a method for the private property, and then use the private property’s generated accessors instead:
With that technique, it’s possible to completely directly setting ivars, and thus avoid -retain
and -release
altogether. (You’ll still need to use -autorelease
at various times, of course, but that’s reasonably rare.) We have some source code files that are well over 2000 lines of code without a single explicit [_ivar retain];
or [_ivar release];
call thanks to this technique. (Yeah, 2000 lines is also large and the class needs refactoring, but that’s another story.)
Of course, you could just use garbage collection which avoids 99% of the need for this bollocks:
- (void)setBackgroundColor:(NSColor*)color { // Yay GC! self->_backgroundColor = color; [view setBackgroundColor:self->_backgroundColor]; }But plenty of us don’t have that luxury yet. (iPhone, ahem.)