KexAri KexAri - 2 months ago 45
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

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 {
    print(addr)
}
Comments