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

Howiieque

macrumors regular
Original poster
Feb 1, 2009
120
0
[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
value.
--The Objective-C 2.0 Programming Language

if it is, what type is it? what is a flag? Could someone give an explanation?:)
 

MacRohde

macrumors regular
Jun 1, 2004
164
0
Copenhagen, Denmark
[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
value.
--The Objective-C 2.0 Programming Language

if it is, what type is it? Could someone give an explanation?:)

From the Objective-C FAQ:

4.2 What is the difference between self and super ?

self is a variable that refers to the object that received a message
in a method implementation. super refers to the same variable, but
directs the compiler to use a method implementation from the
superclass.

Using pseudo-code, where copy (from super) is the syntax for the copy
implementation of the superclass, the following are equivalent :

myObject = [super copy];

and,

myObject = [self copy (from super)]; // pseudo-code
 

Sayer

macrumors 6502a
Jan 4, 2002
981
0
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.
 

Howiieque

macrumors regular
Original poster
Feb 1, 2009
120
0
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.
 

Howiieque

macrumors regular
Original poster
Feb 1, 2009
120
0
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;
@end

@implementation a

- (a*)dd
{
return self;
}
@end


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

// insert code here...

a *b=[[a alloc] init];
NSLog(@"%@\n",b);
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.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 

kpua

macrumors 6502
Jul 25, 2006
294
0
Normally, when you use the message passing syntax like so

Code:
[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:

Code:
[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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.