对于我的游戏的在线模式,我正在使用context
的GKScore
属性,并且由于所有支持Game Center的设备都可以更新到iOS 5(这是在添加context
属性时),因此我要求context
属性可用在线玩。但是,在执行此运行时检查时遇到问题。我以为可以使用[GKScore instancesRespondToSelector:@selector(setContext:)]
来检查它的存在,但是在iOS 5和5.1模拟器以及@selector(context)
上,它返回false。为什么这会发生,请问执行此检查的最干净正确的方法是什么?
最佳答案
我无法完全解释这一点,但是GKScore
类的实例化对象会将 YES 返回给repondsToSelector(context)
,即使该类说不会。如果没有其他解决方案,请构造一个GKScore
对象以进行查询。
我想知道[[GKScore alloc] init]
是否实际上返回GKScore
以外类型的对象。这可能发生。
GKScore *instantiatedScore = [[GKScore alloc] init]; // Add autorelease if using manual reference counting.
NSString* className = NSStringFromClass([instantiatedScore class]);
NSLog(@"instantiatedScore class name = %@", className);
但是,根据此输出,结果并非如此:
instantiatedScore class name = GKScore
我想知道
GKSCore.h
头文件中的编译器指令是否会影响这一点。它定义了两个仅在iOS 5.0或更高版本中可用的属性:context
和shouldSetDefaultLeaderboard
。也许那些编译器指令意味着该类不能保证它将支持这两个属性。在此假设下,
[GKScore instancesRepondToSelector:@selector(category)]
应该返回是,但是[GKScore instancesRepondToSelector:@selector(shouldSetDefaultLeaderboard)]
应该返回 NO 。GKScore *instantiatedScore = [[GKScore alloc] init]; // Add autorelease if using manual reference counting.
NSLog(@"GKScore category = %d", [GKScore instancesRespondToSelector:@selector(category)]);
NSLog(@"instantiatedScore category = %d", [instantiatedScore respondsToSelector:@selector(category)]);
NSLog(@"GKScore context = %d", [GKScore instancesRespondToSelector:@selector(context)]);
NSLog(@"instantiatedScore context = %d", [instantiatedScore respondsToSelector:@selector(context)]);
NSLog(@"GKScore shouldSetDefaultLeaderboard = %d", [GKScore instancesRespondToSelector:@selector(shouldSetDefaultLeaderboard)]);
NSLog(@"instantiatedScore shouldSetDefaultLeaderboard = %d", [instantiatedScore respondsToSelector:@selector(shouldSetDefaultLeaderboard)]);
但是,输出结果比这更奇怪:
GKScore category = 0
instantiatedScore category = 1
GKScore context = 0
instantiatedScore context = 1
GKScore shouldSetDefaultLeaderboard = 1
instantiatedScore shouldSetDefaultLeaderboard = 1
关于objective-c - 检查GKScore实例是否具有上下文属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12126501/