vinh tran
vinh tran

Reputation: 185

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

Hi I want to get an object type dictionary by using NSUserDefaut :

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

but i get the error 'anyObject' is not convertible to 'Dictionary]'

i also try :

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

but it's not work, too. Anyone know how can I do this ? Thanks !

Upvotes: 2

Views: 4001

Answers (1)

Arbitur
Arbitur

Reputation: 39081

Both tries are syntax errors.

To cast to a dictionary: Dictionary<Type, Type> or [Type : Type]

So by doing

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

or

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

Upvotes: 5

Related Questions