user4951
user4951

Reputation: 33140

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

-(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

Upvotes: 13

Views: 440

Answers (2)

rickster
rickster

Reputation: 126167

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.

Upvotes: 11

Catfish_Man
Catfish_Man

Reputation: 41831

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.

Upvotes: 9

Related Questions