PDA

View Full Version : [super negotiate]; Is super a variable or not?




Howiieque
Feb 4, 2009, 03:56 AM
[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
Feb 4, 2009, 04:01 AM
[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 (http://www.faqs.org/faqs/computer-lang/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
Feb 4, 2009, 07:50 AM
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.

kainjow
Feb 4, 2009, 08:17 AM
Apple also discusses this a bit here:
http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/ObjectiveC/Articles/chapter_3_section_5.html

Howiieque
Feb 4, 2009, 09:13 AM
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
Feb 4, 2009, 09:15 AM
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
Feb 4, 2009, 10:35 AM
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
Feb 5, 2009, 11:27 AM
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.

Howiieque
Feb 6, 2009, 10:00 AM
thanks a lot, it's much more clear now.:)