在Objective C中动态调用类方法。

时间:2023-01-15 15:13:03

Suppose I have Objective C interface SomeClass which 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 someMethod on 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 {
   // ???
}

4 个解决方案

#1


83  

Instead of:

而不是:

[someOtherObject invokeSelector:@selector(someMethod) forClass:[SomeClass class];

call:

电话:

[[SomeClass class] performSelector:@selector(someMethod)];

Example (using GNUstep ...)

例子(使用GNUstep…)

file A.h

文件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: "

#2


4  

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 (NSObject or NSProxy in Cocoa).

在Objective-C中,类也是对象。但是,类对象的处理方式不同,因为它们可以调用根类的实例方法(NSObject或Cocoa中的NSProxy)。

So it's possible to use all the instance methods defined in NSObject on class objects as well and the right way to dynamically invoke a class method is:

因此,也可以在类对象上使用NSObject中定义的所有实例方法,动态调用类方法的正确方法是:

[aClass performSelector:@selector(aSelector)];

The apple docs are a bit more specific.

苹果文档更具体一点。

#3


2  

You shouldn't implement this yourself.

你不应该自己实现这个。

The NSObject Protocol has a performSelector: method that does exactly this.

NSObject协议有一个performSelector:方法来实现这一点。

#4


2  

Is this built-in method what you want?

这个内置的方法是你想要的吗?

id objc_msgSend(id theReceiver, SEL theSelector, ...)

(See the runtime reference docs for this function.)

(此函数参见运行时参考文档。)

#1


83  

Instead of:

而不是:

[someOtherObject invokeSelector:@selector(someMethod) forClass:[SomeClass class];

call:

电话:

[[SomeClass class] performSelector:@selector(someMethod)];

Example (using GNUstep ...)

例子(使用GNUstep…)

file A.h

文件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: "

#2


4  

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 (NSObject or NSProxy in Cocoa).

在Objective-C中,类也是对象。但是,类对象的处理方式不同,因为它们可以调用根类的实例方法(NSObject或Cocoa中的NSProxy)。

So it's possible to use all the instance methods defined in NSObject on class objects as well and the right way to dynamically invoke a class method is:

因此,也可以在类对象上使用NSObject中定义的所有实例方法,动态调用类方法的正确方法是:

[aClass performSelector:@selector(aSelector)];

The apple docs are a bit more specific.

苹果文档更具体一点。

#3


2  

You shouldn't implement this yourself.

你不应该自己实现这个。

The NSObject Protocol has a performSelector: method that does exactly this.

NSObject协议有一个performSelector:方法来实现这一点。

#4


2  

Is this built-in method what you want?

这个内置的方法是你想要的吗?

id objc_msgSend(id theReceiver, SEL theSelector, ...)

(See the runtime reference docs for this function.)

(此函数参见运行时参考文档。)