简体   繁体   English

转换为Swift 2.3后对“下标”的模棱两可使用

[英]Ambiguous use of 'subscript' after converting to swift 2.3

I got this Errors after I converting to swift 2.3. 转换为Swift 2.3后出现了此错误。

guard let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary else {
                    throw JSONError.ConversionFailed
                }

                guard
                    let loadedWeather = json["weather"]![0]["description"] as? String,
                    let loadedTemperatur = json["main"]!["temp"] as? Float,
                    let loadedWindSpeed = json["wind"]!["speed"] as? Float
                    else {
                        print("Weather JSON-Parsing failed")
                        return
                }

The Ambiguous use of subscript error comes by declaring "loadedWeather, loadedTemperatur and loadedWindSpeed". Ambiguous use of subscript错误的Ambiguous use of subscript是通过声明“ loadedWeather,loadedTemperatur和loadedWindSpeed”来实现的。

Already tried to change NSDictionary to Dictionary and other things, helped on another position in code, but here.... 已经尝试将NSDictionary更改为Dictionary等,在代码的另一个位置有所帮助,但是在这里。

thanks guys 多谢你们

This happens because compiler doesn't know what are the intermediary object is in each of your line ... so may be 发生这种情况是因为编译器不知道每行中的中间对象是什么...所以可能是

   if let weather = json["weather"] as? [[String:String]], firstObject = weather.first as? [String:String]{
        let loadedWeather = firstObject["description"]
   }

   // same for other objects i.e. `json["main"]` and `json["wind"]` with its return type 

I think that the issue is that the compiler cannot work out what json["weather"] is, You may need to be more specific in your code. 我认为问题在于编译器无法计算出json["weather"]是什么,您可能需要在代码中更加具体。

Try 尝试

let loadedWeather = (json["weather"] as! [[String:AnyObject]])[0]["description"] as? String

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM