necixy
necixy

Reputation: 5064

What's the difference between self.propertyName vs. propertyName?

The title says everything!

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

Upvotes: 0

Views: 209

Answers (6)

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

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.

Upvotes: 2

Abizern
Abizern

Reputation: 150565

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.

Upvotes: 0

Cocoanetics
Cocoanetics

Reputation: 8247

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.

Upvotes: 1

kgutteridge
kgutteridge

Reputation: 8981

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

Upvotes: 2

T.J. Crowder
T.J. Crowder

Reputation: 1073978

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.

Upvotes: 4

Luke
Luke

Reputation: 11476

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

Upvotes: 1

Related Questions