简体   繁体   English

类型'[String:Any]'在Swift中没有下标成员

[英]Type '[String:Any]' has no subscript members in Swift

I am trying to get information from a json result and append and access certain key values. 我试图从json结果中获取信息,并追加和访问某些键值。 However, I am getting the error "Type '[String:Any]' has no subscript members. 但是,我收到错误消息“类型'[String:Any]'没有下标成员。

        let json = try? JSONSerialization.jsonObject(with: data!, options: []) as! [String: Any]
        for case let studentsInfo in json["results"] {
            if let studentInfo = StudentResults(json: result) {
                let name = firstName + " " + lastName
                StudentsResults.append(name)
            }

This is also the struct that I have placed in an extension. 这也是我放置在扩展中的结构。

struct StudentResults{
    let firstName: String
    let lastName: String
    let latitude: Double
    let longitude: Double
    let mapString:String
    let mediaURL: String
    let objectID:String
    let uniqueKey: String
    let updatedAt: String
}

convenience init?(json: [String: Any]) {
    guard let firstName = json["firstName"] as? String,
          let lastName = json["lastName"] as? String
        else {
            return nil
    }
    return nil

This is also my GitHub page for this project if you would like to take a look at it 如果您想看看这个项目,这也是我的GitHub页面

https://github.com/SteveBurgos95/UdacityMapProject https://github.com/SteveBurgos95/UdacityMapProject

It appears to me that the problem is with this line of code: 在我看来,问题出在这行代码上:

let json = try? JSONSerialization.jsonObject(with: data!, options: []) as! [String: Any]

Whilst you are force casting it here as! [String: Any] 当您强制将其投射as! [String: Any] as! [String: Any] , the try? as! [String: Any]try? is giving you an optional value. 给您一个可选的值。

You have two options. 您有两个选择。 You can be a crazy person and change try? 您可以成为一个疯狂的人并try?改变try? to try! try! , or you can take the safer approach and wrap the code like so: ,或者您可以采用更安全的方法并像下面那样包装代码:

do {
    guard let json = try JSONSerialization.jsonObject(with: data!, options: []) as [String: Any] else { return }
    …
} catch {
    // HANDLE ERROR
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 键入any没有下标成员swift 3 - Type any has no subscript members swift 3 将语法转换为swift 3时,类型&#39;Any&#39;没有下标成员吗? - When convert syntax to swift 3, Type 'Any' has no subscript members? 错误:类型“ Any”没有下标成员带有Swift 4的AlamoFire - Error: Type 'Any' has no subscript members AlamoFire with Swift 4 错误:类型&#39;((键:字符串,值:任何)&#39;&#39;没有下标成员带有json - Error: Type '(key: String, value: Any)' has no subscript members with json Alamofire 4.0出现“类型&#39;任何&#39;没有下标成员”错误 - “Type 'Any' has no subscript members” error on Alamofire 4.0 即时修复类型“ NSFastEnumerationIterator.Element”(又名“ Any”)没有下标成员 - Hot to Fix Type 'NSFastEnumerationIterator.Element' (aka 'Any') has no subscript members “输入‘有吗?’ 尝试从嵌套的 JSON 字典初始化对象时,没有下标成员”错误 - "Type 'Any?' has no subscript members " error when trying to initialize objects from a nested JSON dictionary JSON解析错误-类型&#39;RecentTvListData&#39;没有下标成员 - JSON Parsing error - Type 'RecentTvListData' has no subscript members 无法用索引类型“ String”下标“ [[[String:Any]]””的值 - Cannot subscript a value of type '[[String : Any]]' with an index of type 'String' 不能下标类型[Dictionary]的值 <String, Any> ]索引类型为&#39;String&#39; - Cannot subscript a value of type [Dictionary<String, Any>] with an index of type 'String'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM