转换成swift后,使用“下标”2.3 [英] Ambiguous use of 'subscript' after converting to swift 2.3

查看:141
本文介绍了转换成swift后,使用“下标”2.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

guard
let loadWeather = json [weather]![0] [description] as? String,
let loadTemperatur = json [main]![temp] as? Float,
let loadWindSpeed = json [wind]![speed] as? Float
else {
print(Weather JSON-Pararsing failed)
return
}

使用下标错误来自于声明loadedWeather,loadedTemperatur和loadedWindSpeed。



已经尝试将NSDictionary更改为字典和其他东西,帮助另一个代码位置,但这里....



谢谢你们

解决方案

我认为问题是编译器无法解决什么 json [weather] 是,您可能需要在代码中更具体。



尝试

  let loadWeather =(json [weather] as![[String:AnyObject]])[0] [description] as?字符串


I got this Errors after I converting to 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".

Already tried to change NSDictionary to Dictionary and other things, helped on another position in code, but here....

thanks guys

解决方案

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.

Try

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

这篇关于转换成swift后,使用“下标”2.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆