Ios 向NSMutableArray添加函数指针

Ios 向NSMutableArray添加函数指针,ios,objective-c,xcode,nsmutablearray,selector,Ios,Objective C,Xcode,Nsmutablearray,Selector,我需要在NSMutableArray 数组: [_array addObject:@selector(myFunction:)]; - (void) myFunction:(NSString*)someParam {} Incompatible pointer types sending 'SEL' to parameter 'id' 功能: [_array addObject:@selector(myFunction:)]; - (void) myFunction:(NSString*

我需要在
NSMutableArray

数组:

[_array addObject:@selector(myFunction:)];
- (void) myFunction:(NSString*)someParam {}
Incompatible pointer types sending 'SEL' to parameter 'id'
功能:

[_array addObject:@selector(myFunction:)];
- (void) myFunction:(NSString*)someParam {}
Incompatible pointer types sending 'SEL' to parameter 'id'
错误:

[_array addObject:@selector(myFunction:)];
- (void) myFunction:(NSString*)someParam {}
Incompatible pointer types sending 'SEL' to parameter 'id'
我该怎么做/

其目的是随机调用数组函数


关于

您只能在NSArray中存储对象。但是,
SEL
不在其中(其定义为指向
中结构的指针)

为了将选择器存储在数组中,必须将其打包到
NSValue
对象中,如下所示:

[_array addObject:[NSValue valueWithPointer:@selector(myFunction:)]];
SEL mySelector = [_array[0] pointerValue];

[target performSelector:mySelector];
稍后,当您真正想要使用选择器时,需要将其解压缩,如下所示:

[_array addObject:[NSValue valueWithPointer:@selector(myFunction:)]];
SEL mySelector = [_array[0] pointerValue];

[target performSelector:mySelector];

只能在NSArray中存储对象。但是,
SEL
不在其中(其定义为指向
中结构的指针)

为了将选择器存储在数组中,必须将其打包到
NSValue
对象中,如下所示:

[_array addObject:[NSValue valueWithPointer:@selector(myFunction:)]];
SEL mySelector = [_array[0] pointerValue];

[target performSelector:mySelector];
稍后,当您真正想要使用选择器时,需要将其解压缩,如下所示:

[_array addObject:[NSValue valueWithPointer:@selector(myFunction:)]];
SEL mySelector = [_array[0] pointerValue];

[target performSelector:mySelector];

另一种选择是使用块。对于函数

-(void)myFunction{
}
通过执行以下操作创建块:

void (^CallingBlock)()=^(){
    [self myFunction];
};
然后像平常一样添加到数组中

NSMutableArray *array=[[NSMutableArray alloc] init];
[array addObject:CallingBlock];
然后执行该块

[[NSOperationQueue mainQueue] addOperationWithBlock:[array objectAtIndex:0]];

另一种选择是使用块。对于函数

-(void)myFunction{
}
通过执行以下操作创建块:

void (^CallingBlock)()=^(){
    [self myFunction];
};
然后像平常一样添加到数组中

NSMutableArray *array=[[NSMutableArray alloc] init];
[array addObject:CallingBlock];
然后执行该块

[[NSOperationQueue mainQueue] addOperationWithBlock:[array objectAtIndex:0]];

不能将选择器添加到数组中,因为
SEL
不是对象类型


如果需要存储方法和参数,可以创建一个
NSInvocation
对象并将其存储在数组中。

由于
SEL
不是对象类型,因此不能向数组中添加选择器


如果需要存储方法和参数,可以创建一个
NSInvocation
对象并将其存储在数组中。

一个
@selector
不是一个Cocoa“对象”。因此,如何添加指向NSMutableArray的函数指针?创建一个包含该选择器的对象。您可能可以使用NSValue,但我怀疑
@selector
不是简单的指针值。
@selector
不是Cocoa“对象”。因此,如何向NSMutableArray添加函数指针?创建包含该选择器的对象。您可能可以使用NSValue,但我怀疑
@selector
不是一个简单的指针值。只调用
NSStringFromSelector
并存储字符串不是更容易吗?它可以工作,但不会更容易(您仍然需要转换为字符串和转换为字符串)。我更倾向于使用
NSValue
解决方案,因为它看起来更像是保持了相同的类型,只是将其装箱。@jlehr:将指针放入NSValue并将其取出是O(1)。然而,从字符串中获取选择器是O(n)。只调用
NSStringFromSelector
并存储字符串不是更容易吗?这会起作用,但不会更容易(您仍然需要转换为字符串和转换为字符串)。我更倾向于使用
NSValue
解决方案,因为它看起来更像是保持了相同的类型,只是将其装箱。@jlehr:将指针放入NSValue并将其取出是O(1)。然而,从字符串中获取选择器是O(n)。如果不需要参数,则为
NSMethodSignature
。@JoshCaswell:但是如果不需要参数,则它将没有方法的名称或
NSMethodSignature
。@JoshCaswell:但是它将没有方法的名称