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!

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


Justin Cannon
Justin Cannon
3,465 Points

Objective C Pointer Question

Hi! Can someone please help me understand something about pointers in objective c?

I understand that we declare all objects with pointers. I understand why. What I don't understand is how we are able to call methods on what I would have guessed were memory addresses. For example,

NSNumber *foo = @45; [foo someMethod]

foo is a pointer to an NSNumber, right? Shouldn't foo just be a memory address? Why can I call a method on it? From everything I've been learning, I would have guessed this would be the syntax (using * to dereference and get the object value of the pointer):

[*foo someMethod]


2 Answers

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

There's a lot of magic going on behind the scenes. That method call you mentioned above is converted to a C function by the compiler which looks like: objc_msgsend(foo, @selector(someMethod)). If you want to go down that rabbit hole of understanding the Objective-C runtime then read: https://mikeash.com/pyblog/friday-qa-2012-11-16-lets-build-objc_msgsend.html

Matthew Reed
Matthew Reed
Courses Plus Student 17,986 Points

The method is being called on the object that foo is pointed to. I'm guessing they made it that way so you don't have to type an asterisk every time you call a method.