Official Cocoa accessor methods

It must be great to be starting out as a Cocoa programmer these days. I’ve just finished reading the Cocoa Fundamentals Guide and it nicely summarises Cocoa.

One thing that surprised me when I was reading the Guide are the ‘new’ and official accessor methods offered by Apple. The various Cocoa books go into memory management, but each offer different accessor methods. And it’s the same with the Cocoa Fundamentals Guide (and Apple’s Memory Management Programming Guide for Cocoa) – they offer a different way again of writing these!

I’m adopting Apple’s accessor methods in future programs, but thought it might be an interesting idea for a blog post. Chances are, if I didn’t know about it, then others wouldn’t either.

So without further ado, here’s the official accessor methods.

- (NSString*) title
return [[title retain] autorelease];

- (void) setTitle: (NSString*) newTitle
if (title != newTitle) {
[title release];
title = [newTitle retain]; // or copy depending on your needs

You can read about them in the Memory Management Programming Guide for Cocoa.


No comments yet

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: