简体   繁体   English

从NSDictionary存储和检索int64_t:NSNumber vs NSString

[英]Storing and retrieving an int64_t from an NSDictionary: NSNumber vs NSString

I am developing a game with Game Center functionality. 我正在开发一款具有Game Center功能的游戏。 Internally, my game holds all scores in variables of type int64_t , since that is the required type for reporting scores through the GameKit API (I assume this is to avoid ambiguity between 32 bit and 64 bit platforms). 在内部,我的游戏在int64_t类型的变量中保存所有分数,因为这是通过GameKit API报告分数所需的类型(我假设这是为了避免32位和64位平台之间的歧义)。

So far, so good. 到现在为止还挺好。

Next, I wish to store the user's hiscore locally (say, in NSUserDefaults . But a .plist would be equivalent) for when there's no authenticated Game Center player, and also to sync it via iCloud key-value with other devices owned by the user. 接下来,我希望在本地存储用户的hiscore(例如,在NSUserDefaults 。但是.plist是等效的),因为当没有经过身份验证的Game Center播放器时,还要通过iCloud键值与用户拥有的其他设备同步。

I can store the int64_t hiscore like this: 我可以像这样存储 int64_t hiscore:

// (int64_t) _hiscore is an ivar.
// (NSString) _playerID is the authenticated player ID, or else 
//  "AnonymousUser" when GKLocalPlayer is not authenticated.

[playerData setObject:@(_hiscore) forKey:@"HiScore"];
[[NSUserDefaults standardUserDefaults] setObject:playerData forKey:_playerID];
[[NSUserDefaults standardUserDefaults] synchronize];

...(assuming the "@( )" operator will take proper care in all cases - not sure about this either...), but which NSNumber method should I use to retrieve the value without risking integer overflow? ...(假设“@()”运算符将在所有情况下都要小心 - 不确定这个......),但是我应该使用哪个NSNumber方法来检索值而不会冒整数溢出的风险?

All the -*value methods of the class (eg -intValue , -longValue , -unsignedIntegerValue etc.) return types that are platform-dependant (unlike int64_t, which has an explicit byte size). 类的所有-*value方法(例如-intValue-longValue-unsignedIntegerValue等)返回与平台相关的类型(与int64_t不同,它具有明确的字节大小)。 This post gives great info on all the data types on each platform, but it doesn't help with NSNumber ... 这篇文章提供了关于每个平台上所有数据类型的精彩信息,但它对NSNumber没有帮助......

I could do a runtime check of the sizes of the distinguishing types of each platform (eg see if sizeof(unsigned long) equals 4 or 8, and use a different method in each case), but that doesn't look elegant . 我可以对每个平台的区分类型的大小进行运行时检查(例如,查看sizeof(unsigned long)等于4或8,并在每种情况下使用不同的方法),但这看起来并不优雅 Not that a new architecture will appear any time soon, but... 并不是说新架​​构会很快出现,但......

I could also store the number as a string, as described in this post , but having the dictionary-friendly NSNumber at hand, it feels silly... 我还可以将号码存储为一个字符串,如在这个岗位 ,但手头上有字典友好的NSNumber,感觉傻...

What would you recommend? 你会推荐什么?

Use -[NSNumber longLongValue] . 使用-[NSNumber longLongValue] C standard (C99) guarantees long long to be at least 64 bits. C标准(C99)保证long long至少为64位。

By the way, the new @(_hiscore) syntax is equivalent to [NSNumber numberWithLongLong:_hiscore] if _hiscore is int64_t . 顺便说一句,如果_hiscoreint64_t ,新的@(_hiscore)语法相当于[NSNumber numberWithLongLong:_hiscore]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM