我有2个类,Parent和Child,Parent有一个名为func的类方法。 现在我想在func方法中获取Class实例来区分哪个类是调用者。Objective-c:如何在类方法中获得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
}
有什么办法在类方法中获取类实例(或类名)?
我想添加一些实用工具方法(如django中的all())到NSManagedObject。所以我需要区分来电者。 使用'自我'是我想知道的!谢谢! – taichino 2010-06-26 20:07:21
这是不是我谁投票,但我只是检查,并在一个类的方法 自== [自我班] 刚刚看到这已在另一个评论中说过,但澄清,他们是相同的,至少在类方法中。 所以你是对的,我也是如此 – 2010-06-26 20:32:40
Class * class = self;是错的。去掉 *。正确的语法是:Class class = self; – Lukasz 2012-09-21 11:34:18