NSUserDefaults 的特殊字典

Special dictionary to NSUserDefaults

所以我有一个

var userPlaces = [Dictionary<String, String>()]

字典,样本数据为

userPlaces.append(["name":"Schule", "subname":"Wien", "lat":"48.203015", "lon":"16.374419" ])

我想将它保存在 NSUserDefaults.StandarUserDefaults() 中。 我已经尝试使用 .setObjectForKey 和 .setDictionaryForKey,但是当我尝试使用 .objectForKey 重新获得它时,它给出了一个错误 "Cannot assign value of type 'AnyObject?' to type '[Dictionary]'" 有任何想法吗? 提前致谢

你必须投射对象。

[Dictionary] dict = NSUserDefaults.StandardUserDefaults().objectForKey("blah") as! [Dictionary]

它将解决问题:

userPlaces = NSUserDefaults.standardUserDefaults().objectForKey("YourKey") as! [Dictionary< String, String >]

希望对您有所帮助!