I have 2 classes, Parent and Child, and Parent has a class method named func.
Now I want to get Class instance in func method to distinguish which class is caller.
@interface Parent : NSObject
+ (void)func;
@end
@implementation Parent
+ (void)func {
Class *class = howToGetClass();
NSLog(@"%@ call func", class);
}
@end
@interface Child : Parent
@end
int main() {
[Child func]; // call func from Child
}
Is there any way to get class instance (or class name) in class method?
If you just want to log it/get it as a Class, you just need
self. Thats it. So likeor
also, if you want to get the name as an NSString, NSStringFromClass(self) has you covered. (As a char *, class_getName(self) is what you’re looking for)