KexAri KexAri - 1 year ago 193
Swift Question

Type Any has no subscript members json Swift 3

I have this code:

let subjectAbbreviation = JSON?["data"]??["subject"]??["abbr"] as? String

Swift 3 complains:

Type Any has no subscript members

How would I unwrap this? I know I can use
(JSON as? AnyObject)
seems kind of messy though.

Answer Source

You need to specify the type of your json to [String: Any] so try like this.

if let jsondata = json as? [String: Any], let data = jsondata["data"] as? [String: Any], let subject = data["subject"] as? [String: Any], let addr = subject["subject"] as? String {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download