148

在 MyClass.m 中,我定义了

- (void) myTest: (NSString *) withAString{
    NSLog(@"hi, %@", withAString);
}

以及 MyClass.h 中的适当声明。稍后我想打电话

[self performSelector:@selector(mytest:withAString:) withObject: mystring];

在 MyClass.m 但我收到类似于 * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[MyClass myTest:withAtring:]: unrecognized selector sent to instance 0xe421f0'

我尝试了一个更简单的案例,它使用不带参数的选择器,将字符串打印到控制台并且效果很好。代码有什么问题,我该如何解决?谢谢。

4

8 回答 8

315

在 Objective-C 中,选择器的签名包括:

  1. 方法的名称(在本例中为“myTest”)(必需)
  2. 如果方法有输入,则方法名称后面的“:”(冒号)。
  3. 每个附加输入的名称和“:”。

选择器不知道:

  1. 输入类型
  2. 方法的返回类型。

这是一个类实现,其中 performMethodsViaSelectors 方法通过选择器执行其他类方法:

@implementation ClassForSelectors
- (void) fooNoInputs {
    NSLog(@"Does nothing");
}
- (void) fooOneIput:(NSString*) first {
    NSLog(@"Logs %@", first);
}
- (void) fooFirstInput:(NSString*) first secondInput:(NSString*) second {
    NSLog(@"Logs %@ then %@", first, second);
}
- (void) performMethodsViaSelectors {
    [self performSelector:@selector(fooNoInputs)];
    [self performSelector:@selector(fooOneInput:) withObject:@"first"];
    [self performSelector:@selector(fooFirstInput:secondInput:) withObject:@"first" withObject:@"second"];
}
@end

您要为其创建选择器的方法有一个输入,因此您可以为它创建一个选择器,如下所示:

SEL myTestSelector = @selector(myTest:);
于 2009-06-19T17:15:34.153 回答
138

您的方法签名是:

- (void) myTest:(NSString *)

withAString 恰好是参数(名称具有误导性,看起来它是选择器签名的一部分)。

如果您以这种方式调用该函数:

[self performSelector:@selector(myTest:) withObject:myString];

它会起作用的。

但是,正如其他海报所建议的那样,您可能需要重命名该方法:

- (void)myTestWithAString:(NSString*)aString;

并调用:

[self performSelector:@selector(myTestWithAString:) withObject:myString];
于 2009-06-19T16:19:28.187 回答
14

@Shane Arney

performSelector:withObject:withObject:

您可能还想提一下,此方法仅用于传递最多 2 个参数,并且不能延迟。(例如performSelector:withObject:afterDelay:).

有点奇怪,苹果只支持发送 2 个对象,并没有使它更通用。

于 2012-11-05T13:10:38.330 回答
7

您的代码有两个问题。一个被识别并回答了,但另一个没有。首先是您的选择器缺少其参数的名称。但是,即使您修复了该问题,该行仍然会引发异常,假设您修改后的方法签名仍然包含多个参数。假设您修改后的方法声明为:

-(void)myTestWithString:(NSString *)sourceString comparedTo:(NSString *)testString ;

为接受多个参数的方法创建选择器是完全有效的(例如 @selector(myTestWithString:comparedTo:) )。但是,performSelector 方法只允许您将一个值传递给 myTest,不幸的是它有多个参数。它会出错并告诉您您没有提供足够的值。

你总是可以重新定义你的方法来获取一个集合,因为它是唯一的参数:

-(void)myTestWithObjects:(NSDictionary *)testObjects ;

但是,有一个更优雅的解决方案(不需要重构)。答案是使用 NSInvocation 以及它的setArgument:atIndex:invoke方法。

如果您需要更多详细信息,我已经写了一篇文章,包括代码示例。重点是线程,但基础仍然适用。

祝你好运!

于 2009-07-18T15:19:26.487 回答
3

您的方法签名没有意义,您确定这不是错字吗?我不清楚它是如何编译的,尽管您可能会收到您忽略的警告?

您希望此方法采用多少参数?

于 2009-06-19T14:24:29.620 回答
2

认为类应该定义为:

- (void) myTestWithSomeString:(NSString *) astring{
    NSLog(@"hi, %s", astring);
}

你只有一个参数,所以你应该只有一个:

您可能还想考虑在您的 NSLog 中使用 %@ - 这只是一个好习惯 - 然后会写出任何对象 - 而不仅仅是字符串。

于 2009-06-19T14:23:52.507 回答
0

由 android 开发者在 2021_10_15 发布。

(OC比较难用,-_-||)

使用多个参数调用选择器,

你可以使用NSObject performSelector:withObject:withObject

但只支持传递两个参数!!!

幸运的是,你可以performSelector withObject X 3通过objc_msgSend函数来​​实现你。

#include <objc/message.h>

- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2 withObject:(id)object3 {
    typedef id (*send_type)(id, SEL, id, id, id);
    send_type func = (send_type) objc_msgSend;
    id retValue = func(self, aSelector, object1, object2, object3);
    return retValue;
}

用法:

- (NSString *)ObjcMsgSendWithString:(NSString *)string withNum:(NSNumber *)number withArray:(NSArray *)array {
    NSLog(@" ---> %@, %@, %@", string, number, array[0]);
    return @"return 311";
}

- (void)test{
    NSString *str = @"字符串objc_msgSend";
    NSNumber *num = @20;
    NSArray *arr = @[@"数组值1", @"数组值2"];

    SEL sel = @selector(ObjcMsgSendWithString:withNum:withArray:);
    NSLog(@"1223 ---> %@", [self performSelector:sel withObject:str withObject:num withObject:arr]);
}
于 2021-10-15T08:57:34.433 回答
-1

iOS 用户还期望自动大写:在标准文本字段中,区分大小写语言的句子的第一个字母会自动大写。

您可以决定是否实现这些功能;刚刚列出的任何功能都没有专用的 API,因此提供它们是一种竞争优势。

Apple 文档说在自定义键盘中没有可用于此功能和其他一些预期功能的 API。所以你需要找出你自己的逻辑来实现它。

于 2016-01-12T05:43:07.160 回答