开发者

Uploading scores with GameKit

How do I save and upload scores later if there is no connection available? In a WWDC session it says to use the following code if no connection is available:

NSData *archivedScore = [NSKeyedArchiver archivedDataWithRootObject:[NSData dataWithBytes:&score length:sizeof(score)]];
开发者_运维技巧

I then save the NSData object to NSUSerDefaults. But how do I get an int score value back from that to report?

Thanks


Use NSKeyedUnarchiver (from 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜