tai*_*ino 6 objective-c class-method
我有2个类,Parent和Child,Parent有一个名为func的类方法.现在我想在func方法中获取Class实例来区分哪个类是调用者.
@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
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在类方法中获取类实例(或类名)?
Jar*_*tar 16
如果你只是想记录它/把它作为一个类,你只需要self.而已.所以喜欢
+ (void)func {
Class class = self;
NSLog(@"%@ call func", class);
}
Run Code Online (Sandbox Code Playgroud)
要么
+ (void)func {
NSLog(@"%@ call func", self);
}
Run Code Online (Sandbox Code Playgroud)
另外,如果您想将名称作为NSString,NSStringFromClass(self)可以满足您的需求.(作为char*,class_getName(self)就是你要找的)
| 归档时间: |
|
| 查看次数: |
6641 次 |
| 最近记录: |