对于Objective-C ...指向方法的指针

时间:2021-03-18 22:32:51

I want to setup a Method dispatch table and I am wondering if it is possible to create pointer to a method in Objective-C (like pointer to function in C). I tried to use some Objective-C runtime functions to dynamically switch methods but the problem is it will affect all instances.

我想设置一个方法调度表,我想知道是否可以创建指向Objective-C中的方法的指针(如指向C中的函数的指针)。我尝试使用一些Objective-C运行时函数来动态切换方法,但问题是它会影响所有实例。

As I am very new to Objective-C, an illustrated example would be highly appreciated.

由于我是Objective-C的新手,因此非常感谢一个示例。

2 个解决方案

#1


Objective-C methods are called selectors, and are represented by the SEL datatype. If your object inherits from NSObject, you can tell it to perform a selector (i.e. call a method) like thus:

Objective-C方法称为选择器,由SEL数据类型表示。如果你的对象继承自NSObject,你可以告诉它执行一个选择器(即调用方法),如下所示:

SEL selector = @selector(doSomething:);
[obj performSelector:selector withObject:argument];

This assumes you have a method defined such as:

这假定您有一个定义的方法,例如:

-(void)doSomething:(MyObject*)arg;

Selectors are assigned to SEL datatypes through the @selector keyword, which takes the name of the method you would like to keep. The name of the method is the method name stripped of all arguments. For example:

选择器通过@selector关键字分配给SEL数据类型,该关键字采用您想要保留的方法的名称。方法的名称是剥离了所有参数的方法名称。例如:

-(void)doSomething:(MyObject*)arg withParams:(MyParams*)params

Would be referenced as @selector(doSomething:withParams:).

将被引用为@selector(doSomething:withParams :)。

#2


Yes! In Objective-C, function pointers are called selectors. If you have a method defined like this:

是!在Objective-C中,函数指针称为选择器。如果你有一个像这样定义的方法:

- (void)myFunctionWithObject:(NSObject*)obj otherObject:(NSNumber*)obj2
{
}

The selector is declared like this:

选择器声明如下:

@selector(myFunctionWithObject:otherObject:)

To perform a selector on an object, you can use:

要在对象上执行选择器,您可以使用:

[object performSelector:@selector(myFunction)];

or

[object performSelector:@selector(myFunctionTakingParameter:) withObject: o];

The selector data type is particularly useful for threads and timers, where you can dispatch a thread and provide it a selector to the message you'd like it to invoke. If you need to create an array of selectors (or a dispatch table), or if you need to invoke selectors with multiple parameters, you can use the NSInvocation class. It provides a wrapper for a selector and allows you to specify actual arguments.

选择器数据类型对于线程和计时器特别有用,您可以在其中调度线程并为您希望调用的消息提供选择器。如果需要创建选择器数组(或调度表),或者需要调用具有多个参数的选择器,则可以使用NSInvocation类。它为选择器提供了一个包装器,允许您指定实际的参数。

You should keep in mind that Objective-C is already based on a fully dynamic method dispatch table. It sounds like maintaining function pointers using selectors will work fine for you if you just need a reference to a function, though.

您应该记住,Objective-C已经基于一个完全动态的方法调度表。听起来像使用选择器维护函数指针,如果你只需要一个函数的引用,它将适合你。

#1


Objective-C methods are called selectors, and are represented by the SEL datatype. If your object inherits from NSObject, you can tell it to perform a selector (i.e. call a method) like thus:

Objective-C方法称为选择器,由SEL数据类型表示。如果你的对象继承自NSObject,你可以告诉它执行一个选择器(即调用方法),如下所示:

SEL selector = @selector(doSomething:);
[obj performSelector:selector withObject:argument];

This assumes you have a method defined such as:

这假定您有一个定义的方法,例如:

-(void)doSomething:(MyObject*)arg;

Selectors are assigned to SEL datatypes through the @selector keyword, which takes the name of the method you would like to keep. The name of the method is the method name stripped of all arguments. For example:

选择器通过@selector关键字分配给SEL数据类型,该关键字采用您想要保留的方法的名称。方法的名称是剥离了所有参数的方法名称。例如:

-(void)doSomething:(MyObject*)arg withParams:(MyParams*)params

Would be referenced as @selector(doSomething:withParams:).

将被引用为@selector(doSomething:withParams :)。

#2


Yes! In Objective-C, function pointers are called selectors. If you have a method defined like this:

是!在Objective-C中,函数指针称为选择器。如果你有一个像这样定义的方法:

- (void)myFunctionWithObject:(NSObject*)obj otherObject:(NSNumber*)obj2
{
}

The selector is declared like this:

选择器声明如下:

@selector(myFunctionWithObject:otherObject:)

To perform a selector on an object, you can use:

要在对象上执行选择器,您可以使用:

[object performSelector:@selector(myFunction)];

or

[object performSelector:@selector(myFunctionTakingParameter:) withObject: o];

The selector data type is particularly useful for threads and timers, where you can dispatch a thread and provide it a selector to the message you'd like it to invoke. If you need to create an array of selectors (or a dispatch table), or if you need to invoke selectors with multiple parameters, you can use the NSInvocation class. It provides a wrapper for a selector and allows you to specify actual arguments.

选择器数据类型对于线程和计时器特别有用,您可以在其中调度线程并为您希望调用的消息提供选择器。如果需要创建选择器数组(或调度表),或者需要调用具有多个参数的选择器,则可以使用NSInvocation类。它为选择器提供了一个包装器,允许您指定实际的参数。

You should keep in mind that Objective-C is already based on a fully dynamic method dispatch table. It sounds like maintaining function pointers using selectors will work fine for you if you just need a reference to a function, though.

您应该记住,Objective-C已经基于一个完全动态的方法调度表。听起来像使用选择器维护函数指针,如果你只需要一个函数的引用,它将适合你。