objective-c - 检查GKScore实例是否具有上下文属性

标签 objective-c ios game-center

对于我的游戏的在线模式,我正在使用contextGKScore属性,并且由于所有支持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或更高版本中可用的属性:contextshouldSetDefaultLeaderboard。也许那些编译器指令意味着该类不能保证它将支持这两个属性。

在此假设下,[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/

相关文章:

ios - Watchkit OS 2,WCSession 在后台运行

objective-c - NSString 在发布时崩溃

html - iOS:从文档目录读取html文件

ios - 如何在 iOS 7 中通过游戏中心应用程序邀请玩家?

objective-c - 通过关键字查找相关音频

objective-c - Siri 听写绕过 UITextView :shouldChangeTextInRange Delegate

iOS Realm 检测 RLMObject 的变化

ios - Testflight 无法安装应用程序

ios - 游戏中心排行榜不工作 ("no scores")

iphone - 来自底部的 OpenFeint 通知