当我从确实实现了我要调用的方法的对象中调用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"];
}

最佳答案

"mySelector:withCustomObject:"是具有2个参数的方法的签名,例如

- (void)mySelector:(id)firstArgument withCustomArgument:(id)secondArgument { ... }


但是,您调用performSelector:withObject:,它将发送一个仅包含mySelector参数的消息。第二个参数未定义,可能会导致崩溃。

因此,如果mySelector实际上有2个参数,请使用performSelector:withObject:withObject:,否则修复选择器的签名。

关于iphone - 不良访问权限performSelector:WithObject:方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12062428/

10-10 20:49