Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Flamehearted

macrumors newbie
Original poster
Aug 4, 2010
4
0
Hi all,

Would anyone be able to give me an idiot proof description of Self and Super in objective C - I am struggling to understand these two from what I have read so far.

Thanks in advance,

Martin
 
self refers to the "current" instance of the class in question. It is a pointer back to the object. super refers to the instance of the superclass (the class this class inherits from). Note that these two instances are actually one object but the super pointer allows us to execute overridden methods.
 
super is an aspect of Polymorphism in OO speak.

If Object B is derived from Object A, calling super from an instance of Object B would get you the same instance but all of your methods and attributes would be from Object A. So if Object B overrides any methods from Object A, calling "super" would allow you to get the behavior from Object A.
 
There is a description in this article, at the heading "Messages to self and super":
http://developer.apple.com/mac/libr...#//apple_ref/doc/uid/TP30001163-CH12-BAJHDGAC

  • self searches for the method implementation in the usual manner, starting in the dispatch table of the receiving object’s class. In the example above, it would begin with the class of the object receiving the reposition message.
  • super starts the search for the method implementation in a very different place. It begins in the superclass of the class that defines the method where super appears. In the example above, it would begin with the superclass of the class where reposition is defined.
You should read the complete original description, including its examples, then ask specific questions if something doesn't make sense.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.