Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

XCode/Objective-C

Hi I have been trying to get my head around Xcode & Objective-C and have seen a few sample codes where (associated with Getters & Setters) the name of the property in the code changes from e.g: example to _example . Can anyone explain why this is?

Thanks Larry

2 Answers

This is just a naming convention. As of the latest version of Xcode, properties autosynthesize instance variables prefixed with an underscore:

@property (nonatomic, strong) NSString *myProp --> ivar = _myProp

In previous versions, properties automatically created instance variables with the same name as the property. The change was done to prevent users from inadvertently modifying an instance variable without going through its getter/setter. For reasons such as memory implications, you should always access an instance variable via its property.

If you @synthesize a property named myProp and try to access it via "myProp", you will get a compiler error. You need to access it either via it's property (self.myProp) or it's instance variable directly (_myProp)

conventions