【发布时间】:2014-10-24 05:23:36
【问题描述】:
我正在尝试在游戏结束时显示一条消息,显示玩家是否获胜。
以下是相关代码:
BOOL yes = YES;
NSString *winMessage = [NSString stringWithFormat:@"You win!"];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(endGameWithMessage:win:par:)]];
[inv setSelector:@selector(endGameWithMessage:win:par:)];
[inv setTarget:self];
[inv setArgument:&winMessage atIndex:2];
[inv setArgument:&yes atIndex:3]; //this is the win BOOL (0 and 1 are explained in the link above)
[inv setArgument:&parBool atIndex:4]; //this is the par BOOL
[inv performSelector:@selector(invoke) withObject:nil afterDelay:0.1f];
这里是 endGameWithMessage 方法签名:
- (void)endGameWithMessage:(NSString*)message win:(BOOL)win par:(BOOL)parBool
尽管像这样直接调用代码(显然不允许延迟)工作得很好,消息按预期显示并且不会导致任何崩溃:
[self endGameWithMessage:@"You win!" win:YES par:parBool];
尝试使用 NSInvocation 会导致 endGameWithMessage: 方法出现 EXC_BAD_ACCESS 崩溃。这是否意味着我以错误的方式将值传递给方法调用?
【问题讨论】:
标签: ios objective-c selector exc-bad-access nsinvocation