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

I expected [super class] return a superclass class, however I found using this code to return this class class.

the code

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

Exit

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

Can someone explain why this is happening, 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) 
 
+6
source share
3 answers

[super class] calls the super method in the current instance (ie self ). If I had an overridden version, then it would be named, and it would look different. Since you do not override it, calling [self class] same as calling [super class] .

+6
source

super refers to the implementation of a method in a superclass. If you do not 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 .

+2
source

While mprivat and Sulthan's answers are technically correct, the current case is a bit more complicated (and interesting):

Consideration of the implementation of a method call. It uses (of course) an object pointer. An object pointer is a pointer to a structure that has as its first element ( isa ) a pointer to a class of objects. The class methods (i.e., as mprivat and Sulthan stated correctly, the same in both cases) follows this pointer to define the class, i.e. Always the class of the caller .

As a result, if A is a superclass of B, both calls to [self class] and [super class] called by instance of B will return class B ( not of A, which might be expected due to a missing redefinition!).

0
source

All Articles