在Objective-C中向选择器传递参数

时间:2023-01-15 18:25:01

My function looks like this

函数是这样的

-(void) doTransition:(id)sender withID:(int) n

How do I pass int n to selector,

如何将int n传递给selector,

target:self selector:@selector(doTransition:)withID:3

this won't work

这是行不通的

Please help Thanks

请帮助谢谢

3 个解决方案

#1


3  

The short answer is that you don't. methods like performSelector: are set up to pass NSObjects as parameters, not scalar values.

简而言之,你没有。像performSelector:的方法被设置为将NSObjects作为参数传递,而不是标量值。

There is a method performSelector:withObject:withObject: that will invoke a method with 2 object parameters.

有一个方法performSelector:withObject:withObject:它将调用一个具有两个对象参数的方法。

You can trigger a method with any type of parameters by creating an NSInvocation, but it's a fair amount of work, and a little tricky to figure out how to do it.

您可以通过创建nsinvoc来触发具有任何类型参数的方法,但是这是相当多的工作,而且要知道如何去做也有点棘手。

In your case it would be simpler to refactor your method to take the ID parameter as an NSNumber object.

在您的情况下,重构您的方法以将ID参数作为NSNumber对象是比较简单的。

#2


5  

So, I Googled "calling selectors with arguments" and got a bunch of results, the first of which is a question here on SO: Objective-C: Calling selectors with multiple arguments

我在谷歌上搜索了"调用带参数的选择器"得到了一堆结果,第一个问题是关于。Objective-C:调用带有多个参数的选择器

The most voted answer there has some details, specifically:

投票最多的答案有一些细节:

[selectorTarget performSelector:mySelector withObject:@3];

This is a simple task to perform. More information can be found at the linked question/answer area.

这是一个简单的任务。更多的信息可以在链接的问题/答案区域找到。

Notice the @3 I passed, which is not an int but an NSNumber. If you have an int value you can easily get an NSNumber by performing [NSNumber numberWithInt:myIntValue]. The @3 is shorthand syntax available in clang 3.4 for creating an NSNumber with an integer value. The use of NSNumber here instead of int is important because this selector call expects an id type as the withObject: parameter and so thus I need an object, and not a primitive type (whether the compiler auto-wraps for you, I can't say).

注意我传递的@3,它不是int,而是NSNumber。如果您有一个int值,您可以通过执行[NSNumber withint:myIntValue]轻松获得NSNumber。@3是clang 3.4中可用的用于创建具有整数值的NSNumber的简写语法。这里使用NSNumber而不是int是很重要的,因为这个选择器调用需要一个id类型作为withObject: parameter,因此我需要一个对象,而不是一个原始类型(无论编译器是否自动为您包装,我不能说)。

Another important note is that there are three performSelector: functions defined:

另一个重要的注意事项是有三个performSelector:函数定义:

- (id)performSelector:(SEL)aSelector;
- (id)performSelector:(SEL)aSelector withObject:(id)anObject;
- (id)performSelector:(SEL)aSelector withObject:(id)anObject withObject:(id)anotherObject;

So you can see you can use functions that don't take values and functions that take one or two values as selectors but outside of that you'd most likely need to write your own logic for calling functions with greater than 2 arity.

你可以使用不取值的函数和取一两个值作为选择符的函数但除此之外,你可能需要编写自己的逻辑来调用大于2的函数。

#3


0  

You need to pass a NSDictionary with the values you want to send, something like:

您需要传递一个带有想要发送的值的NSDictionary,如下:

NSDictionary *myDictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:3] forKey:@"myInt"];

#1


3  

The short answer is that you don't. methods like performSelector: are set up to pass NSObjects as parameters, not scalar values.

简而言之,你没有。像performSelector:的方法被设置为将NSObjects作为参数传递,而不是标量值。

There is a method performSelector:withObject:withObject: that will invoke a method with 2 object parameters.

有一个方法performSelector:withObject:withObject:它将调用一个具有两个对象参数的方法。

You can trigger a method with any type of parameters by creating an NSInvocation, but it's a fair amount of work, and a little tricky to figure out how to do it.

您可以通过创建nsinvoc来触发具有任何类型参数的方法,但是这是相当多的工作,而且要知道如何去做也有点棘手。

In your case it would be simpler to refactor your method to take the ID parameter as an NSNumber object.

在您的情况下,重构您的方法以将ID参数作为NSNumber对象是比较简单的。

#2


5  

So, I Googled "calling selectors with arguments" and got a bunch of results, the first of which is a question here on SO: Objective-C: Calling selectors with multiple arguments

我在谷歌上搜索了"调用带参数的选择器"得到了一堆结果,第一个问题是关于。Objective-C:调用带有多个参数的选择器

The most voted answer there has some details, specifically:

投票最多的答案有一些细节:

[selectorTarget performSelector:mySelector withObject:@3];

This is a simple task to perform. More information can be found at the linked question/answer area.

这是一个简单的任务。更多的信息可以在链接的问题/答案区域找到。

Notice the @3 I passed, which is not an int but an NSNumber. If you have an int value you can easily get an NSNumber by performing [NSNumber numberWithInt:myIntValue]. The @3 is shorthand syntax available in clang 3.4 for creating an NSNumber with an integer value. The use of NSNumber here instead of int is important because this selector call expects an id type as the withObject: parameter and so thus I need an object, and not a primitive type (whether the compiler auto-wraps for you, I can't say).

注意我传递的@3,它不是int,而是NSNumber。如果您有一个int值,您可以通过执行[NSNumber withint:myIntValue]轻松获得NSNumber。@3是clang 3.4中可用的用于创建具有整数值的NSNumber的简写语法。这里使用NSNumber而不是int是很重要的,因为这个选择器调用需要一个id类型作为withObject: parameter,因此我需要一个对象,而不是一个原始类型(无论编译器是否自动为您包装,我不能说)。

Another important note is that there are three performSelector: functions defined:

另一个重要的注意事项是有三个performSelector:函数定义:

- (id)performSelector:(SEL)aSelector;
- (id)performSelector:(SEL)aSelector withObject:(id)anObject;
- (id)performSelector:(SEL)aSelector withObject:(id)anObject withObject:(id)anotherObject;

So you can see you can use functions that don't take values and functions that take one or two values as selectors but outside of that you'd most likely need to write your own logic for calling functions with greater than 2 arity.

你可以使用不取值的函数和取一两个值作为选择符的函数但除此之外,你可能需要编写自己的逻辑来调用大于2的函数。

#3


0  

You need to pass a NSDictionary with the values you want to send, something like:

您需要传递一个带有想要发送的值的NSDictionary,如下:

NSDictionary *myDictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:3] forKey:@"myInt"];