Parth Adroja Parth Adroja - 1 year ago 66
iOS Question

How can I access some object from `Any` Type?

In Swift 2.2, I used to write like this

let dict = [String: AnyObject]()
let value = dict.objectForKey("price") as! Double

But as
is now replaced with type
how can I access dictionary values as no method for
for type Any.
Do i need to type cast to

Answer Source

First of all you can't use objectForKey for swift native type. you can simply use subscript as i mentioned in comments and also use if let for safely unwrap.

 let dict: [String: Any] = ["price": 2.0 ]
 if let value = dict["price"] as? Double {

Note: As always, do not use NSMutableDictionary in Swift. this type lack type information

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download