1

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

1 回答 1

5

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

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

但是你打电话performSelector:withObject:,它发送一条只有一个参数的消息mySelector。第二个参数是未定义的,这可能会导致崩溃。

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

于 2012-08-21T20:41:19.207 回答