Objective-c: How can I get Class instance in class method

前端 未结 2 1036
耶瑟儿~
耶瑟儿~ 2020-12-20 17:46

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.



        
2条回答
  •  余生分开走
    2020-12-20 18:27

    If you just want to log it/get it as a Class, you just need self. Thats it. So like

    + (void)func {
        Class class = self;
        NSLog(@"%@ call func", class);
    }
    

    or

    + (void)func {
        NSLog(@"%@ call func", self);
    }
    

    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)

提交回复
热议问题