使用GameKit上传分数

时间:2011-01-19 13:27:45

标签: iphone nsdata gamekit game-center

如果没有可用的连接,如何保存和上传分数?在WWDC会话中,如果没有可用的连接,则表示使用以下代码:

NSData *archivedScore = [NSKeyedArchiver archivedDataWithRootObject:[NSData dataWithBytes:&score length:sizeof(score)]];

然后我将NSData对象保存到NSUSerDefaults。但是如何从报告中得到一个int得分值?

谢谢

1 个答案:

答案 0 :(得分:1)

使用NSKeyedUnarchiver(来自Archives and Serializations Programming Guide

int score = 42;
NSData *archivedScore = [NSKeyedArchiver archivedDataWithRootObject:[NSData dataWithBytes:&score length:sizeof(score)]];
int *scorePtr = [[NSKeyedUnarchiver unarchiveObjectWithData:archivedScore] bytes];
NSLog(@"score = %d", *scorePtr); // Output: score = 42