gpt4 book ai didi

ios - 将函数指针添加到 NSMutableArray

转载 作者:行者123 更新时间:2023-11-28 18:32:54 25 4
gpt4 key购买 nike

我需要在 NSMutableArray 中添加一些函数

数组:

[_array addObject:@selector(myFunction:)];

函数:

- (void) myFunction:(NSString*)someParam {}

错误:

Incompatible pointer types sending 'SEL' to parameter 'id'

我该怎么做? :/

目的是随机调用数组函数

问候

最佳答案

你只能在 NSArray 中存储对象。但是SEL不在其中(它被定义为指向 <objc/objc.h> 中结构的指针)。

为了让您的选择器存储在数组中,您必须将它打包在一个 NSValue 中。对象,像这样:

[_array addObject:[NSValue valueWithPointer:@selector(myFunction:)]];

稍后,当您真正想要使用您的选择器时,您需要将其解包,如下所示:

SEL mySelector = [_array[0] pointerValue];

[target performSelector:mySelector];

关于ios - 将函数指针添加到 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24514088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com