当我从一个实现了我试图调用的方法的对象调用performSelector:withObject:时,我得到了一个EXC_BAD_ACCESS异常。这是我的代码
SEL newSelector = NSSelectorFromString(@"mySelector:withCustomObject:");
[self performSelector:newSelector withObject:myCustomObject];
这会导致崩溃。但是,当我这样做的时候
[self performSelector:@selector(mySelector:withCustomObject:) withObject:myCustomObject];
它起作用了。
你知道为什么会发生这样的事情吗?PS:没有一个参数是空的。
更多代码:
// My code to call this method
SEL newSelector = NSSelectorFromString(@"mySelector:withCustomObject:");
[self performSelector:newSelector withObject:self withObject:myCustomObject];
// this code is NOT called.
- (void) mySelector:(jObject *)sender withCustomObject:(jEvent *)customObject
{
NSDictionary *handlerData = [aProperty objectAtIndex:[event positionInMethodStack]];
NSString *newTitle = [handlerData objectForKey:@"newTitle"];
}
发布于 2012-08-21 20:41:19
"mySelector:withCustomObject:"
是具有2个参数的方法的签名,例如
- (void)mySelector:(id)firstArgument withCustomArgument:(id)secondArgument { ... }
但是您调用performSelector:withObject:
,它向mySelector
发送一条只有一个参数的消息。第二个参数未定义,这可能会导致崩溃。
因此,如果mySelector
实际上有两个参数,则使用performSelector:withObject:withObject:
,否则修复选择器的签名。
https://stackoverflow.com/questions/12062428
复制相似问题