Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-05-2009, 09:35 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 27
Default When to use "self.objectName" or just "objectName"

Trying to understand when it is needed to prepend object names with self.

Sometimes I am able to reference objects that I have initialized without adding self. and other times my code won't work without adding it.

Can someone explain when I should definitely use the self. reference and when it is not needed?
jj0b is offline   Reply With Quote
Old 05-05-2009, 10:56 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by jj0b View Post
Trying to understand when it is needed to prepend object names with self.

Sometimes I am able to reference objects that I have initialized without adding self. and other times my code won't work without adding it.

Can someone explain when I should definitely use the self. reference and when it is not needed?
Whenever you use self.blah or someObject.blah, you are using a "property". Properties are an easy way to call certain methods to get and set the value of a variable inside the class (an instance variable.)

In other words, when you say self.blah = true, it's shorthand for [self setBlah:true]. You either need to write the setBlah method yourself, or use @synthesize to get the compiler to write them for you. The setBlah method may copy the object you give it, or retain it, or simply assign it, depending on the settings you chose for the property.

When you simply say blah=true, you are not using the property, and you are not calling the setBlah method. You are simply changing the value of the variable, with no retain or object copy or any other tricks.

If you look up "properties" in your favorite book or reference you'll find out more.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 05-06-2009, 08:34 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 495
Default

Quick addendum: When you define a property the critical places you will want to use self.property ( to my knowledge ) are : IF the property variable is an object pointer ( not a primative like int or BOOL ) and you are setting the value.

Code:
@property (nonatomic, readwrite) int someValue;
@property (nonatomic, retain) UIView *someView;

// these two are equivalent
someValue = 3;
self.someValue = 3;

// these two are NOT 
someView = newView;
self.someView = newView; // use this one

// shouldn't matter when *getting* the value
newView = someView;
newView = self.someView;
If you look into a basic tutorial on writing properties yourself it should become fairly apparent what happens behind the scenes when you use the @property/@synthesize statements, which should make it more obvious which cases you need to use self ( to go through the property accessors ).

=)
__________________
My Apps on AppStore : gScale (guitar scales reference), eMaze, eMaze Lite, eTimesheet
exorcyze is offline   Reply With Quote
Old 05-06-2009, 11:24 AM   #4 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: San Diego, CA
Posts: 406
Default

Actually, the two forms are NOT equivalent!

self.foo = calls the setter method.
foo does not.

Depending on the options you gave @synthesize, the setter may have memory-management side-effects.
jtara is offline   Reply With Quote
Old 05-06-2009, 11:33 AM   #5 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 495
Default

If you read my post, you'll see that I am absolutely NOT indicating that they are always equivalent. The example provided was for a primative ivar that had it's @property option set to readwrite.

[edit]
Again, you will want to use the self prefix when you want to SET a value for an object variable who's @property declaration indicated extra memory notations ( retain / copy ). In other circumstances it's syntactical fluff as far as I'm aware.

It's true that prefixing with self calls the setter method. But the point is that in cases where no retain/copy/release is being performed in the setter ( IE when you specify assign, readwrite, etc for nonatomic properties ) that the two are functionally performing the same thing behind the scenes.

=)
__________________
My Apps on AppStore : gScale (guitar scales reference), eMaze, eMaze Lite, eTimesheet

Last edited by exorcyze; 05-06-2009 at 11:44 AM.
exorcyze is offline   Reply With Quote
Old 05-06-2009, 12:15 PM   #6 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by exorcyze View Post
It's true that prefixing with self calls the setter method. But the point is that in cases where no retain/copy/release is being performed in the setter ( IE when you specify assign, readwrite, etc for nonatomic properties ) that the two are functionally performing the same thing behind the scenes.
Usually but not always; I think jtara's point is that there are other situations where the setter and getter might do something other than directly set the ivar.

For example, I might have two properties, angleInRadians and angleInDegrees, but only one ivar for angle, and write getters and setters to modify that ivar. In that case, "self.angleInRadians = blah" would be correct, and just "angleInRadians = blah" would be an error.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 05-06-2009, 01:09 PM   #7 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 495
Default

Totally true in the case where you write your own properties. I was refering only to the cases where you use the @property / @synthesize method for generating the accessors. =)
__________________
My Apps on AppStore : gScale (guitar scales reference), eMaze, eMaze Lite, eTimesheet
exorcyze is offline   Reply With Quote
Old 05-06-2009, 02:18 PM   #8 (permalink)
wdn
Registered Member
 
Join Date: Sep 2008
Posts: 28
Default

If you're going to use properties, good programming practice dictates that you always go through your accessor methods. The slightly more verbose syntax makes your code more readable by clearly distinguishing and identifying the scope of public properties from instance (which may be private) or local variables. The additional overhead is also negligible unless you're in a tight loop, in which case you can use a local variable inside the loop and accessor methods around it.

Always using accessor methods can help you with debugging. You can monitor a property which is acting strangely by overriding the synthesized accessors, testing for invalid values and breaking as necessary. This may be easier than using watchpoints in gdb for complex cases.

If you always go through accessor methods, your code will be ready for interesting KVC technologies that make Cocoa so powerful, such as KVO, object archiving, and eventually Core Data and Cocoa Bindings (hopefully). If you directly access your properties, you bypass the runtime mechanism for hooking into these technologies.

Quote:
Originally Posted by exorcyze
It's true that prefixing with self calls the setter method. But the point is that in cases where no retain/copy/release is being performed in the setter ( IE when you specify assign, readwrite, etc for nonatomic properties ) that the two are functionally performing the same thing behind the scenes.
Note that this will not work if you're using KVO, say, to keep your UI in sync with your model objects, even for simple properties such as assigned scalars. This is important because you have no control over who may want to observe a property you've declared, which is public by definition. Being a good citizen means always going through the accessor so any observers can be properly notified. This isn't an issue for code you entirely own, but if you ever plan to release it as part of a framework or collaborate with others, it's best to follow good habits from the start.
wdn is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 158,484
Threads: 89,092
Posts: 380,130
Top Poster: BrianSlick (7,091)
Welcome to our newest member, lavernwatts
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:53 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0