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

jcfiesta

macrumors newbie
Original poster
Mar 23, 2013
3
0
Hi,

I'm new here and new in studying iOS dev't.

I have a Label and when I drag it to my ViewController header file it automatically creates a property. However, when I checked my implementation file there was no synthesize part. I thought it should be automatic? What do you think I'm doing wrong?
 
If that's the case, how come my property cannot be recognize in the implementation code?

----------

Oh wait. I think I get it now. All I need to do is reference it with an underscore prefix.
 
If that's the case, how come my property cannot be recognize in the implementation code?

----------

Oh wait. I think I get it now. All I need to do is reference it with an underscore prefix.

Sort of, but no.

Using the underscore lets you access the underlying auto-created instance variable. Don't do that.

Instead, use the property:

Code:
[self myProperty]
or

Code:
self.myProperty

There are limited times when you should use the iVar directly (custom getters and setters and possibly in the dealloc method).
 
If that's the case, how come my property cannot be recognize in the implementation code?

1. Post your code. Enough of it so someone else can compile it.

2. Post the error message. Copy and paste the complete actual text, and identify where it occurs in the source.

3. Post your OS and Xcode versions.
 
why do we need to reference it to self if we are in the class itself? kindly clarify. thanks!

Sort of, but no.

Using the underscore lets you access the underlying auto-created instance variable. Don't do that.

Instead, use the property:

Code:
[self myProperty]
or

Code:
self.myProperty

There are limited times when you should use the iVar directly (custom getters and setters and possibly in the dealloc method).
 
why do we need to reference it to self if we are in the class itself? kindly clarify. thanks!

Because if you don't, you are circumventing the accessors (getters & setters). You may not understand these terms right now, but such circumventions can have implications with subclassing, KVO and atomicity.

Or, if you've set up a custom getter (for example: to lazy-load some value into the instance variable and that operation is expensive), accessing the instance variable directly may not contain any value, whereas the custom getter can guarantee one.
 
Because if you don't, you are circumventing the accessors (getters & setters). You may not understand these terms right now, but such circumventions can have implications with subclassing, KVO and atomicity.

Or, if you've set up a custom getter (for example: to lazy-load some value into the instance variable and that operation is expensive), accessing the instance variable directly may not contain any value, whereas the custom getter can guarantee one.

What he said.

Basically, until you fully understand the difference between using an ivar and property, you should use the property. The only time you should access your ivars directly, until you get more advanced, is in the code of a custom getter/setter, or possibly in your dealloc method.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.