更新时间:2023-02-01 19:38:40
有几种方法可以做到这一点。
There is a few ways to do this.
我更喜欢使用带有NSCoding的Singleton GameData类来容纳所有需要永久保存的属性(数组,字典等)。
I prefer to use a singleton GameData class with NSCoding to house all the properties (arrays, dictionaries etc) that need to be saved permanently.
我之所以喜欢它,是因为它使代码更具可读性,并且具有可以从项目中任何位置访问属性的优点。这样,还很容易包含icloud键值存储。
I like it this way because it makes code more readable and also has the advantage that you can access the properties from anywhere in your project. This way it's also easy to include icloud key value storage.
您可以阅读我回答的这两个问题,以简单地了解其外观
You can read these 2 questions I answered for a simple example of how this can look
将自定义类保存到NSUserDefaults中,是否有比使用NSCoder编码和解码所有内容更好的方法?
这里也有一些不错的教程
There is a also few good tutorials around such as this
https://www.hackingwithswift.com/read/12/3/fixing-project-10-nscoding
要安全保存数据,您可以/应该使用钥匙串。要了解更多信息,请查看此问题。
To securely save data you can/should use keychain. To read more about it check out this question.