好吧,我正在创建一个自定义SEL,例如:

NSArray *tableArray = [NSArray arrayWithObjects:@"aaa", @"bbb", nil];
for ( NSString *table in tableArray ){
    SEL customSelector = NSSelectorFromString([NSString stringWithFormat:@"abcWith%@", table]);
    [self performSelector:customSelector withObject:0];
}


我收到一个错误:
由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[Sync aaaWithaaa]:无法识别的选择器已发送到实例

但是,如果我使用真实的方法名称运行它,它将起作用!

[self performSelector:@selector(aaaWithaaa:) withObject:0];


如何解决呢?

最佳答案

您已经从字符串创建了选择器-将其传递给performSelector:方法:

[self performSelector:customSelector withObject:0];


编辑:请记住,如果您的方法采用参数,则在从中创建选择器时必须使用冒号:

// Note that you may need colon here:
[NSString stringWithFormat:@"abcWith%@:", table]

关于objective-c - Objective-C:如何执行performSelector:@selector?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10073824/

10-12 04:41