'anyObject' 不可转换为 'Dictionary<key, value>]'

'anyObject' is not convertible to 'Dictionary<key, value>]'

您好,我想使用 NSUserDefaut 获取对象类型字典:

var favoritePlace = NSUserDefaults.standardUserDefaults().objectForKey("savePlace")!  as [Dictionary]

但我收到错误 'anyObject' is not convertible to 'Dictionary]'

我也试试 :

var favoritePlace = NSUserDefaults.standardUserDefaults().objectForKey("savePlace")!  as [Dictionary<String, String>()]

但这也行不通。 任何人都知道我该怎么做?谢谢 !

两次尝试都是语法错误。

转换为字典:Dictionary<Type, Type>[Type : Type]

所以通过

var favoritePlace = NSUserDefaults.standardUserDefaults().objectForKey("savePlace")! as [[String: String]]

var favoritePlace = NSUserDefaults.standardUserDefaults().objectForKey("savePlace")! as [Dictionary<String, String>]