[super negotiate]; Is super a variable or not?

Discussion in 'Mac Programming' started by Howiieque, Feb 4, 2009.

  1. Howiieque macrumors regular

    Feb 1, 2009
    [super negotiate]; Is super a variable or not?
    To do this, you can send a message to super, a special variable in the Objective-C language.
    --learning cocoa with objective-c 2nd

    super is simply a flag to the compiler telling it where to begin searching for the method to perform; it’s used only as the receiver of a message. But self is a variable name that can be used in any number of ways, even assigned a new
    --The Objective-C 2.0 Programming Language

    if it is, what type is it? what is a flag? Could someone give an explanation?:)
  2. MacRohde macrumors regular


    Jun 1, 2004
    Copenhagen, Denmark
    From the Objective-C FAQ:

  3. Sayer macrumors 6502a


    Jan 4, 2002
    Austin, TX
    It might be easier to think of "super" as a "parent" of your class. Your class is the "child" of the "parent" class.

    Also "super" can mean above, the class above you.

    Self is a shortcut to mean the class you are inside of now similar to "this" in other object-oriented languages.
  4. Howiieque thread starter macrumors regular

    Feb 1, 2009
    thank you. and I had watched the link, but not quite understand how things work, although i know how to use them. I just want to know a little deeper.
  5. Howiieque thread starter macrumors regular

    Feb 1, 2009
    it seems that self is pointer points to itself, for the init method simply return self.
    i used an silly programme to verify it:
    #import <Foundation/Foundation.h>

    @interface a : NSObject


    - (a*)dd;

    @implementation a

    - (a*)dd
    return self;

    int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // insert code here...

    a *b=[[a alloc] init];
    NSLog(@"%@\n",[b dd]);
    [pool drain];
    return 0;

    and if i changed the self to super, error occurred. is there anyone has a idea?

    further, the init method from NSObject just return self. why it must send init before using the instance. in this case, if i just remove the init, nothing happened. the init method is to ensure everything to be initialized, if you have this pattern:
    - (id)init {
    if (self = [super init]) {
    //your code
    return self;
    am i right? give a comment.
  6. kainjow Moderator emeritus


    Jun 15, 2000
    super isn't used the same way self is used. self is used as a normal variable that represents the current instance of the class. You can pass self to other methods and access it like any other instance. super is used only for calling the superclass's implementation of a method.

    The init method is the default designated initialization method for an object. Once you call alloc (from my understanding), the memory is allocated and all instance variables are initialized to 0. The init method sets up the object with initial values. The return type by default is self but can be different, or could be nil. It's kind of a verification method, to ensure data is valid. For example, if I had an initWithURL: method that takes an NSURL, I could return nil if the url is not a file url.
  7. kpua macrumors 6502

    Jul 25, 2006
    Normally, when you use the message passing syntax like so

    [anObject message];
    the compiler translates this into a call to the runtime function objc_msgSend(), which dynamically finds the implementation for -message on the given object. It starts looking for the implementation at the object's actual class and moves up the class hierarchy if it can't find an implementation. (If it doesn't find one, it throws an exception.)

    When you do this:

    [super message];
    the compiler translates this into a call to objc_msgSendSuper(). This does essentially the same thing as objc_msgSend(), except for two things: The object receiving the message is always self, and instead of starting to look for the method implementation in the object's actual class, it beings its search at its superclass.

    That's what the second book meant by a 'flag' to the compiler. The compiler knows that when a message is sent to super, it should use objc_msgSendSuper() instead of the regular obj_msgSend().

    The compiler only allows the use of super as the receiver of message. It is not a variable at all, which is why you got an error when trying to replace self with super.
  8. Howiieque thread starter macrumors regular

    Feb 1, 2009

Share This Page