我有一个接受NSString*
参数和BOOL
类型参数的函数:
-(void)getIdByName:(NSString*)name shouldAsk:(BOOL)should {
// I see this log "should = -24", why -24?
NSLog(@"should = %d", should);
...
}
我想使用performSelector:withObject:withObject:
调用上述函数。
我首先在NSArray
:
// 1st parameter is string "abc", 2nd parameter is boolean NO
NSArray * args = [NSArray arrayWithObjects: @"abc", @NO, nil];
我叫功能:
[self executeFunc:@selector(getIdByName:shouldAsk:) withArgs:args];
executeFunc:withArgs:
看起来像这样:
// This is a general function
-(void)executeFunc:(SEL)selector withArgs:(NSArray*)args {
...
// I see this log prints "2nd arg is 0",which looks correct.
NSLog(@"2nd arg is %d", [args objectAtIndex:1]);
[obj performSelector:selector
withObject:[args objectAtIndex:0]
withObject:[args objectAtIndex:1]];
}
当我在executeFunc:
函数上面运行时,我会看到登录:
2nd arg is 0
所以布尔值是@NO,没关系。
但是在正在执行的功能中,日志显示:
should = -24
布尔参数的值为-24
,为什么?如何使用正确的布尔变量传递到执行中的函数?
====更新===
我还尝试重构一般函数以使用NSInvocation
方式:
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[obj methodSignatureForSelector:aSelector]];
[invocation setSelector:aSelector];
[invocation setTarget:obj];
for (int i = 0; i < [arguments count]; i++) {
// compiler error: Cannot take the address of rvalue of type 'id'
[invocation setArgument:&([arguments objectAtIndex:i]) atIndex:2+i];
}
它在我的场景中不起作用。所以,我不认为我的问题与@Sulthan标记的问题重复。