Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


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.