Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Schnigges

macrumors newbie
Original poster
Feb 22, 2010
27
0
Germany
Hi,

I started learning Objective-C a few weeks ago. By doing a lot of tutorials I already understood the main purpose of properties in Objective-C, but there are a few things, which are not quiet clear yet.
First question would be when it's better to only use a property instead of using a property + an instance variable, which are named the same.
Secondly, why would you name instance variable + property differently and then assign them in the @synthesize - part,
e.g: @synthesize window = _window

Thanks,
Schnigges
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
On the iPhone runtime you don't have the option of a declared property and no variable. So don't worry about that. Basically the option of not having the variable was added to the, so-called, "modern" runtime.

As for why you'd use a variable with a different name? Basically to prevent you assigning values directly to the variable as if you do this the setter does not get called and any side effects (like retain) don't get called.
 

Schnigges

macrumors newbie
Original poster
Feb 22, 2010
27
0
Germany
Thanks for the quick reply.
Regarding my second question...is the default behaviour that the setter is called when variable and property have the same names?
And how about IBOutlets / IBActions as properties...I've seen code where an instance variable was used, in another tutorial it wasn't. Are there any specific situations where using both should be preferred and where not?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
This calls the setter
Code:
myObject.property = value;

From an instance method this calls the setter
Code:
self.property = value;

This does not call the setter (ever)
Code:
property = value;

I would suggest always ensuring the setter gets called.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.