【发布时间】:2012-07-30 10:16:04
【问题描述】:
我有一个有趣的问题,我试图在一个类上调用类方法,而我在我的测试方法中基本上一无所知。我可以检查它的继承和它可能实现的任何协议,但看不到一种简单的方法来调用它的方法而不被 NSInvocation 束缚。下面的代码虽然很粗略,但试图证明我遇到的问题。
@interface ClassA : NSObject
+ (Class)classIsPartialClassOf;
@end
@implementation ClassA
+ (Class)classIsPartialClassOf {
return [NSString class];
}
@end
@interface ClassB : NSObject
@end
@implementation ClassB
- (id)init {
[ClassB testClass:[ClassA class]];
}
+ (void)testClass:(Class)classDecl {
/* obviously if you know the type you can just call the method */
[ClassA classIsPartialClassOf];
/* but in my instance I do not know the type, obviously there are no classmethods to perform selector such as the fictional one below */
[classDecl performSelector:@selector(classIsPartialClassOf)];
}
@end
获取实现的方法似乎返回实例变体,我无法让它们在静态类本身上触发。
我的选择是否仅限于调用,还是我错过了一些明显的东西,应该踢自己?
提前感谢您的帮助。
【问题讨论】:
-
在 Obj-c 中它被称为
class方法,而不是static方法。区别很重要。
标签: objective-c selector performselector