Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why [super class] and [self class] produce the same result?

Tags:

objective-c

-(NSString *) nibName
{
    PO([self class]);
    PO([super class]);
    PO([self superclass]);

Only the [self superclass] produces the actual super class.

[self class]: BGImageForBizs
[super class]: BGImageForBizs
[self superclass]: BGUIImageWithActivityIndicator
like image 425
user4951 Avatar asked Feb 07 '13 02:02

user4951


People also ask

Is super() necessary Python?

In general it is necessary. And it's often necessary for it to be the first call in your init. It first calls the init function of the parent class ( dict ). It typically creates its underlying data structure.

What does super() return?

An Overview of Python's super() Function super() alone returns a temporary object of the superclass that then allows you to call that superclass's methods.


2 Answers

Sending a message using super is the same as sending it to self, but explicitly uses the superclass' method implementation. But your class probably doesn't implement the -class method itself, so you're already inheriting the implementation of -class from your superclass already.

In fact, you're probably inheriting NSObject's implementation of -class (even if NSObject isn't your immediate superclass; -class isn't a method that gets overriden often). And the default implementation just returns the contents of the object's isa pointer, telling you what Class it is.

In other words, it's important to recognize that calling super doesn't completely transform your object into an instance of its superclass; it just runs the superclass' code for that method. If there's any introspection going on in that code, it still sees your object as an instance of whatever class it started out as.

like image 177
rickster Avatar answered Sep 20 '22 22:09

rickster


self and super are the same object. The only thing super does is control which implementation of the method is called. Since both your class and the superclass are using NSObject's implementation of -class, the same method is called in both cases.

like image 40
Catfish_Man Avatar answered Sep 18 '22 22:09

Catfish_Man