这是我收到的错误消息:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__ NSCFString authenticationChanged]:无法识别的选择器已发送到实例0x176769a0”
首先抛出调用堆栈:
(0x30496ecb 0x3ac31ce7 0x3049a7f7 0x304990f7 0x303e8058 0x30458f01 0x303ccd69 0x30db8cc5 0x3102f43b 0x3b11ad53 0x3b11ad3f 0x3b11d6c3 0x30461641 0x30cab003300303
libc++ abi.dylib:以类型为NSException的未捕获异常终止
我将游戏中心集成到了应用中,这可能是导致崩溃的代码:
- (id)init {
if ((self = [super init]))
{
gameCenterAvailable = [self isGameCenterAvailable];
if (gameCenterAvailable) {
NSNotificationCenter *nc =
[NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(authenticationChanged)
name:GKPlayerAuthenticationDidChangeNotificationName
object:nil];
}
}
return self;
}
- (void)authenticationChanged {
if ([GKLocalPlayer localPlayer].isAuthenticated && !userAuthenticated) {
NSLog(@"Authentication changed: player authenticated.");
userAuthenticated = TRUE;
} else if (![GKLocalPlayer localPlayer].isAuthenticated && userAuthenticated) {
NSLog(@"Authentication changed: player not authenticated");
userAuthenticated = FALSE;
}
最佳答案
此问题最可能的原因是,您永远不会在需要时删除观察者。
将以下内容添加到您的 class 中:
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
这样可以确保不再注册旧对象以接收通知。
附带说明,不要重复代码。您的
authenticationChanged
方法会更好:- (void)authenticationChanged {
if ([GKLocalPlayer localPlayer].isAuthenticated) {
userAuthenticated = !userAuthenticated;
NSLog(@"Authentication changed: player %@authenticated.", userAuthenticated ? @"" : @"not ");
}
}
并确保将
YES
或NO
与BOOL
变量一起使用。关于ios - 当我按下主页按钮然后恢复它时,ios7应用程序崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24897982/