Type Any has no subscript members json Swift 3

梦想与她 提交于 2020-08-06 04:38:15

问题


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.


回答1:


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)
}


来源:https://stackoverflow.com/questions/39740847/type-any-has-no-subscript-members-json-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!