Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "[self class] == [super class]"?

I expected [super class] to return the superclass's class, however I found, using this code that it returns this class's class.

Code

NSLogObject([self class]);
NSLogObject([super class]);
NSLogObject([self superclass]);
NSLogBool([self class] == [super class]);

Output

[self class]: MainMenuScene
[super class]: MainMenuScene
[self superclass]: CCScene
[self class] == [super class]:[YES]

Can somebody explain why this happens please?. I expect it to return the same value as [self superclass].

Macros:
-------
#define NSLogBool(i)   NSLog(@"%s:[%@]", #i, (i) ? @"YES" : @"NO")
#define NSLogObject(o) NSLog(@"%s:[%@]", #o, o)
like image 613
James Webster Avatar asked Aug 06 '12 11:08

James Webster


2 Answers

[super class] calls the super method on the current instance (i.e. self). If self had an overridden version, then it would be called and it would look different. Since you don't override it, calling [self class] is the same as calling [super class].

like image 180
mprivat Avatar answered Sep 20 '22 01:09

mprivat


super refers to the method implementation in the superclass. If you don't override the method in your class, the implementation in your class and its superclass is the same.

Both [super class] and [self class] call the same method, defined on NSObject.

like image 29
Sulthan Avatar answered Sep 21 '22 01:09

Sulthan