0

The title says everything!

In Objective-C, what's the difference between self.propertyName vs. propertyName?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
necixy
  • 4,964
  • 5
  • 38
  • 54
  • 2
    Is the search function broken? This must have been asked and answered dozens of times already. Check out the related posts over here for a few -------> – Roger Jun 18 '11 at 10:21
  • @roger, try pasting the same title and question in ask question page and you will find out yourself. :) – necixy Jun 18 '11 at 10:53
  • 2
    .. well it doesn't look broken from where I am ;-) – Roger Jun 18 '11 at 12:00

6 Answers6

4

self.propertyName is sending the object a message, asking it for the value of propertyName, which means it may go through a getter/setter, etc. propertyName is directly accessing the ivar, bypassing any getter/setter. Here's an article going into it in rather more detail.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

self.propertyName increse the retain count by one if you have specified the propertyName as retain in property declaration

propertyName will not increase the retain count an could lead to the crash of application.

e. g. ,

@property (nonatomic,retain) NSString* propertyName;

lets say you have nameProperty NSString object. Below increase the retain count by 1 and you could use self.propertyName and call release.

self.propertyName = nameProperty;
[nameProperty release];

Below does'nt increase the retain count so if you use propertyName in your application it will result in crashing of your application.

propertyName = nameProperty;
[nameProperty release]; 

Any further use of propertyName will result in crash.

Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
  • It doesn't do any memory management for non-object types such as `BOOL` or `NSInteger` or `CGFloat` etc. – Abizern Jun 18 '11 at 10:11
  • @Abizern : why downvote ? Not a valid reason, Read answer carefully i given the example for NSObjects with retain in property and it's accepted too :( , Just ask yourself the significant of having `retain` with basic type... – Jhaliya - Praveen Sharma Jun 18 '11 at 10:23
  • Because you've answered a specific case, not the entire case. There are more differences than just `retain`ing an object. Also - the reference to `retainCount` is misleading. Don't think of the retain count of the object, just worry about releasing objects that you own. What if the object that you are passing in is a static string? There is no increase in its `retainCount`, but it has been `retained`. – Abizern Jun 18 '11 at 10:29
  • 2
    Saying that `self.propertyName` increases the retain count is just wrong. It doesn’t increase the retain count when used as a getter. It also doesn’t increase the retain count when the object being assigned is the same as the one that’s currently stored in the backing instance variable. –  Jun 18 '11 at 10:34
  • @Abizern : Yes, i know `retainCount` could give surprise result because of possibility of retained by other framework objects, I just use `retain count` to make the user understand , I didn't mentioned about the **retainCount** method for verification. – Jhaliya - Praveen Sharma Jun 18 '11 at 10:34
  • Also, the rationale that not using `self.propertyName` can lead to a crash is weird. There are use cases where using `self.propertyName` can also lead to a crash, so what’s your real point? –  Jun 18 '11 at 10:38
  • @Bavarious : when `self.propertyName` is assigned with the value then **setter** function is invoked by framework and retain by the `propertyName`. Plz check the assignment operator ..., I didn't specify increment of the retain count when used as a getter. Please read the answer carefully ... – Jhaliya - Praveen Sharma Jun 18 '11 at 10:38
  • @Bavarious : No offense dude, But it seems me you are confused with the memory management in objective-C, Plz read once again ... – Jhaliya - Praveen Sharma Jun 18 '11 at 10:40
  • 1
    Hey, I still couldn't figure out why voted down? What jhaliya gave was a perfect answer to the situation and he didn't given example of accessing an object and didn't even said that by accessing the object retain count will, its an obvious thing. I think we MUST encourage community member for their support not to vote down for what HE DIDN'T do. I request all community members to support others for their help. Vote down privilege is provided only to respected members and it should be used VERY CAREFULLY. +100 for jhaliya (I wish I could vote up 100 times). :) – necixy Jun 18 '11 at 10:52
  • 2
    @Guru it's not a perfect answer IMO, and although you think it's useful, the next person who comes along and reads this answer will think it's perfect unless the *community* gives the answer a rating. Secondly, downvotes are *not punishment* they affect rep much less than an upvote. And if you learn one thing from this discussion its that you shouldn't think of an object's `retainCount`, only whether or not it's your responsibility to call `release` on it. – Abizern Jun 18 '11 at 11:03
  • 1
    Check this http://stackoverflow.com/questions/5743899/initializing-mkmapview-starts-with-retain-count-2/5744085#5744085 – Jhaliya - Praveen Sharma Jun 18 '11 at 11:08
2

self. runs through your likely synthesized accessor methods if you are using properties

ie self.propertyName = newName is the same as [self setPropertyName:newName]

This becomes important for memory management as propertyName = newName would cause you to loose reference to the previous contents of propertyName

kgutteridge
  • 8,727
  • 1
  • 18
  • 23
1

If you call self, you can be sure you're calling the class/object that owns the property.

You may find this useful too:

Assigning to self in Objective-C

Community
  • 1
  • 1
Luke
  • 11,426
  • 43
  • 60
  • 69
1

dot notation is turned into a method call by the compiler. This means that there is extra work at run time for executing this method call, like copying something from and to the stack memory and executing a jump in machine code.

the instance variable by itself is faster because it is essentially just a memory address or scalar value (like int).

One would prefer the self.something notation when you want or need an extra layer to do something. Like retain an object that is passed in or lazily instantiate an object on the first time you need it.

Cocoanetics
  • 8,171
  • 2
  • 30
  • 57
0

Setting the value of the property does just that - it sets the value of the property directly without going through any accessors or synthesized accessors.

By calling the accessor through self you are going through the accessors. For properties that have been declared with retain or copy it will retain or copy the value that is passed in. For non objecte properties, the usual declaration is assign which means that there is no memory management applied to those iVars.

You see both types of calls - but it is preferred to use the direct method in initialisers and the dealloc method, because calls to self are discouraged in these methods.

If you have declared and synthesized the property, the call to self also generates the KVO notifications for changes in that variable. This saves you having to write the willChangeValueForKey: and didChangeValueForKey: methods.

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • No offense, plz don't use such word like **retaliation** here . there is nothing personal from my side, not sure at you end ,Plz do'nt mislead people here,we are here to support the community developers, to give them a better answer not a summery of answers, – Jhaliya - Praveen Sharma Jun 18 '11 at 11:04