简体   繁体   English

用钥匙扣保存分数

[英]Saving score with Keychain

I am developing a game with Spritekit and I'm now at a point where I have to keep track of the user's highscore. 我正在用Spritekit开发游戏,现在我必须跟踪用户的高分。 There are various different methods for storage with the easiest one being NSUserDefaults, but it's recommend for preferences. 有多种不同的存储方法,最简单的一种是NSUserDefaults,但建议使用首选项。

However, I found this Keychain wrapper which tends to save a string with a key: 但是,我发现了这个钥匙串包装器,它倾向于用钥匙来保存字符串:

https://github.com/jrendel/SwiftKeychainWrapper https://github.com/jrendel/SwiftKeychainWrapper

Do you think it's good to use it? 您认为使用它很好吗? My idea is to convert the score (Integer) to a String before saving it and while retrieving the highscore, I will convert it back from String to Int. 我的想法是在保存之前将比分(Integer)转换为String,并在检索高分时将其从String转换回Int。 Do you think it's a good idea? 您认为这是个好主意吗?

The main advantages of the key chain I can see are that entries persists when an app is deleted and can't easily be changed. 我看到的钥匙链的主要优点是,删除应用程序后条目仍然存在,并且不容易更改。 UserDefaults would be tied to your app Id and lost on app delete: unless you used the global domain. UserDefaults将与您的应用程序ID绑定,并在应用程序删除时丢失:除非您使用了全局域。

You could look to use CloudKit key/value storage to achieve cross device high scores, storing the data in the cloud. 您可能希望使用CloudKit键/值存储来实现跨设备的高分,将数​​据存储在云中。 This would be more work and you would need to add conflict resolution. 这将需要更多的工作,您将需要添加冲突解决方案。

Depending on how serious you are taking high scores, you should perhaps consider integrating with Game Center? 根据您获得高分的认真程度,您也许应该考虑与Game Center集成? Ideal if you want high score etc to persist across more than one device. 如果您希望高分等在多个设备上持续存在,则是理想之选。

https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/LeaderBoards/LeaderBoards.html https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/LeaderBoards/LeaderBoards.html

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

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