objective-c 在 Objective C 中动态调用类方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1781046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamically invoke a class method in Objective C
提问by GregK
Suppose I have Objective C interface SomeClasswhich has a class method called someMethod:
假设我有一个 Objective C 接口SomeClass,它有一个名为 的类方法someMethod:
@interface SomeClass : NSObject {
}
+ (id)someMethod;
@end
In some other interface I want to have a helper method that would dynamically invoke someMethodon a class like this:
在其他一些接口中,我想要一个辅助方法来动态调用someMethod这样的类:
[someOtherObject invokeSelector:@selector(someMethod) forClass:[SomeClass class];
What should be the implementation for invokeSelector? Is it possible at all?
应该实现invokeSelector什么?有可能吗?
- (void)invokeSelector:(SEL)aSelector forClass:(Class)aClass {
// ???
}
回答by stefanB
Instead of:
代替:
[someOtherObject invokeSelector:@selector(someMethod) forClass:[SomeClass class];
call:
称呼:
[[SomeClass class] performSelector:@selector(someMethod)];
Example (using GNUstep ...)
示例(使用 GNUstep ...)
file A.h
档案啊
#import <Foundation/Foundation.h>
@interface A : NSObject {}
- (NSString *)description;
+ (NSString *)action;
@end
file A.m
文件是
#import <Foundation/Foundation.h>
#import "A.h"
@implementation A
- (NSString *)description
{
return [NSString stringWithString: @"A"];
}
+ (NSString *)action
{
return [NSString stringWithString:@"A::action"];
}
@end
Somewhere else:
别的地方:
A *a = [[A class] performSelector:@selector(action)];
NSLog(@"%@",a);
Output:
输出:
2009-11-22 23:32:41.974 abc[3200] A::action
nice explanation from http://www.cocoabuilder.com/archive/cocoa/197631-how-do-classes-respond-to-performselector.html:
来自http://www.cocoabuilder.com/archive/cocoa/197631-how-do-classes-respond-to-performselector.html 的很好的解释:
"In Objective-C, a class object gets all the instance methods of the root class for its hierarchy. This means that every class object that descends from NSObject gets all of NSObject's instance methods - including performSelector:."
“在 Objective-C 中,类对象获取其层次结构的根类的所有实例方法。这意味着从 NSObject 派生的每个类对象都获取所有 NSObject 的实例方法 - 包括 performSelector:。”
回答by Adrian
In Objective-C, classes are objects as well. The class objects are treated differently, however, as they can call the instance methods of their root class (NSObjector NSProxyin Cocoa).
在 Objective-C 中,类也是对象。然而,类对象的处理方式不同,因为它们可以调用其根类(NSObject或NSProxy在 Cocoa 中)的实例方法。
So it's possible to use all the instance methods defined in NSObjecton class objects as well and the right way to dynamically invoke a class method is:
所以也可以使用NSObject类对象上定义的所有实例方法,动态调用类方法的正确方法是:
[aClass performSelector:@selector(aSelector)];
The apple docsare a bit more specific.
在苹果文档的更具体一点。
回答by Ben S
You shouldn't implement this yourself.
你不应该自己实现这个。
The NSObject Protocol has a performSelector:method that does exactly this.
NSObject 协议有一个performSelector:方法可以做到这一点。
回答by vanja.
Is this built-in method what you want?
这是您想要的内置方法吗?
id objc_msgSend(id theReceiver, SEL theSelector, ...)
(See the runtime reference docsfor this function.)
(请参阅此函数的运行时参考文档。)

