我有一个简单的函数,用于将一个对象保存到用户默认设置中,以便稍后加载它,函数如下:
func saveGame() {
// hero is an instance of the class Hero, subclassed from SKSpriteNode
UserDefaults.standard.set(hero, forKey: HeroObjectKey)
}
我遇到了这个错误:
2017-03-13 21:15:33.124271 Castle[5405:5208658] [用户默认设置] 尝试将非属性列表对象 name:'hero' texture:[ 'hero1.ico' (32 x 32)] position:{0, 0} scale:{1.00, 1.00} size:{32, 32} anchor:{0.5, 0.5} rotation:0.00 作为NSUserDefaults/CFPreferences值保存到键heroObjectKey中。
这是因为我不能保存自定义对象,还是因为对象内的某些属性值不被接受?
注:有一个非常相似的问题部分解决了我的问题,但那里的被接受答案对我没有用,因为Swift3和他们使用的东西之间存在差异。所以我会在这里提供答案。