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

Boon Kiat Seah
Boon Kiat Seah
66,664 Points

Refactoring into a Model - Extra Credit Questions

Hi

In the below context, what is the difference between specifying the NSArray *predictions in the CrystalBall.h file as _predictions and predictions ?

@interface THCrystalBall : NSObject { NSArray *_predictions; } instead of

@interface THCrystalBall : NSObject { NSArray *predictions; } Is the underscore any significance in this context? When i am specifying as just *predictions and making all the necessary changes to the implementation folder as well, i was able to compile and run the app successfully.

There does not seem to be any difference, which is the best iOS practices?

Regards,

Boon Kiat

1 Answer

If you are creating a instance variable to use directly like you seem to be doing in the example it makes no difference, but iOS best practices is not to underscore the "instance variable". It use to be good practice to assign underscores to property names so you could tell them apart from the instance variable, for example: predictions = _predictions; but that is not really common either as most people use self.property to access properties. The underscore in properties is still used it is just happening under the hood for you. Again for your question I have heard that Apple themselves specifically says not to underscore instance variables, but you will still see people doing it as a habit from other languages mostly. There are Objective-C coding convention best practices guides that are all different in some ways so it is a debate that continues.

Boon Kiat Seah
Boon Kiat Seah
66,664 Points

That 's very helpful . Thanks