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 Build a Simple iPhone App (iOS7) Refactoring into a Model Understanding @property

Shaun Johnson
Shaun Johnson
1,214 Points

NSArray *_predictions;

The video is unclear as to the definition of the array pointer NSArray *_predictions; and accessing the instance variable _predictions. _predictions is accessed directly, "_predictions", and is not a pointer. So why the pointer notation in the explicit declaration?

3 Answers

Stone Preston
Stone Preston
42,016 Points

_predictions is of type NSArray, which means it is an object. All objects in objective c are declared as pointers.

Shaun Johnson
Shaun Johnson
1,214 Points

Thanks, that makes sense. The code goes on to access "_predictions" (if (_predictions == nil)) directly. Can you lend some insight into how this works, given _predictions is a pointer to an object?

Stone Preston
Stone Preston
42,016 Points

are you asking why you didnt have to dereference the pointer like:

if (*_predictions == nil)
Shaun Johnson
Shaun Johnson
1,214 Points

Yes. I NSLog'd _predictions and it is in fact the array I defined. I was expecting the address of the object.

Stone Preston
Stone Preston
42,016 Points

ok thats because when using NSLog and passing it a %@ specifier, what that really does is send the description message to that object. the description message (and most other messages) accepts a pointer to an objects not the object itself. So thats why

 printf(@"Predictions: %@", _predictions)

doesnt print the memory address of _predictions since the description method accepts a pointer as its argument, not actual objects themselves. Objective c methods do all that stuff for you behind the scenes so you dont usually have to dereference object pointers because of the way the language is designed to work. Its a lot more complicated than that simple explanantion, but you get the idea.