When To Use self.myObject Instead Of myObject In iPhone Programming

I have seen a lot of confusion on the topic of when and why to reference an object using self.myObject versus just plain myObject. It took me a while to understand this myself, and please, if I am mistaken in my explanation, set me right, but here it is as I understand it.

Apparently some people don't like Objective-C

If you reference an object in your implementation that you declared in your header with @property and then used @synthesize in your implementation to automatically create getter and setter methods for the object, then using self.myObject  = newObject will call the setter and references to self.myObject will call the getter. As far as I know, if you are just referencing your object you don’t need to use the getter unless you are trying to distinguish from an object with the same name. When you assign your object however, the distinction becomes very important because everywhere you write:

self.myObject = newObject;

you are actually sending the message:

[self setMyObject:newObject];

which under the hood looks like:

objc_msgSend(self, @selector(setMyObject:), newObject);

The consequence of using self.myObject in this way is that myObject will use whatever retain property you set in the @property declaration. So if you set up myObject like so:

@property (nonatomic, retain) MyObjectClass *myObject;

then the value of myObject will be retained even after newObject has been deallocated.

If instead you had assigned myObject like this:

myObject = newObject;

then myObject is just the same as newObject. Therefore if newObject is deallocated then myObject will be pointing to a meaningless memory address. Probably not what you want.

So, here is an example:

.h file
...
UIView *myUIView;
...
@property (nonatomic, retain) UIView *myUIView;
...

.m file
...
@synthesize myUIView;
...
// Get frame:
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];

// Set up myUIView:
UIView *tempUIView = [[UIView alloc] initWithFrame:applicationFrame];
tempUIView.backgroundColor = [UIColor blackColor];
self.myUIView = tempUIView;
[tempUIView release];
...

At this point in the code myUIView is now set up and ready to use. Because I used the setter to assign myUIView it is unaffected when I release tempUIView. If I had forgotten to use self.myUIView for the assignment then I would be unable to use myUIView later in the program.

Hope that clears things up for some of you. Please comment if I have something wrong or if you can help make things even clearer.

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Technorati
  • LinkedIn
  • RSS
  • Slashdot
  • Twitter

About Jason Job

I am a technologist, music producer and an obsessive student of the Internet. I create sound, write code and make things with electronics and microcontrollers. I work as a software developer and consultant and am currently focused primarily on mobile applications for iPhone, iPad and iPod Touch.
This entry was posted in Objective-C, code, iPhone. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>