[英]NSInvocation with an enum?
------------ EDIT ------------- ------------编辑-------------
TL;DR: TL; DR:
It seems I used the NSInvocation class incorrectly (lacking pointer knowledge). 看来我错误地使用了NSInvocation类(缺少指针知识)。
The way to use those would be like so (Works): 使用这些方法的方式如下所示(工作):
NSString *str = @"string";
UIControlState state = UIControlStateNormal;
[invocation setArgument: &str atIndex: 2];
[invocation setArgument: &state atIndex: 3];
--------------- Original Question --------- ---------------原始问题---------
Is it possible to setArgument:atIndex:
with an enum argument? 可以用枚举参数设置setArgument:atIndex:
吗?
See the following example (that doesn't work): 请参见以下示例(无效):
UIButton *btn = ...;
SEL selector = @selector(setTitle:forState:);
NSInovcation *invocation = [NSInovcation invocationWithMethodSignature: [btn methodSignatureForSelector: selector]];
[invocation setSelector: selector];
[invocation setArgument: @"Test" atIndex: 2];
//Nothing happens
UIControlState state = UIControlStateNormal;
[invocation setArgument: &state atIndex: 3];
//Runtime error (the pointer is NULL)
UIControlState *state = UIControlStateNormal;
[invocation setArgument: state atIndex: 3];
//No errors but no effect
int state2 = 0;
[invocation setArgument: &state atIndex: 3];
//Compile error (casting)
[invocation setArgument: @(UIControlStateNormal) atIndex: 3];
//Runtime EXC_BAD_ACCESS
[invocation setArgument: (void *)@(UIControlStateNormal) atIndex: 3];
...
[invocation invokeWithTarget: btn];
Strangely (or not), it does work well with performSelector:...
: 奇怪的是(或不是),它与performSelector:...
一起使用时效果很好:
[btn performSelector: selector withObject: @"Testing" withObject:@(UIControlStateNormal)];
I'm not sure I'm using NSInovcation the correct way. 我不确定我是否以正确的方式使用NSInovcation。 Anyone would like to elaborate? 有人想详细说明吗?
try this: 尝试这个:
NSString *test = @"Test";
[invocation setArgument: &test atIndex: 2];
UIControlState state = UIControlStateNormal;
[invocation setArgument: &state atIndex: 3];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.