转换为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 }
Ambiguous use of subscript
错误的Ambiguous use of subscript
来自声明“loadedWeather,loadedTemperatur和loadedWindSpeed”。
已经尝试将NSDictionary更改为字典和其他的东西,帮助在代码中的另一个位置,但在这里….
多谢你们
我认为问题在于编译器无法计算出json["weather"]
是什么,您可能需要在代码中更加具体。
尝试
let loadedWeather = (json["weather"] as! [[String:AnyObject]])[0]["description"] as? String
发生这种情况是因为编译器不知道中间对象在每行中是什么…所以可能是这样
if let weather = json["weather"] as? [[String:String]], firstObject = weather.first as? [String:String]{ let loadedWeather = firstObject["description"] } // same for other objects ie `json["main"]` and `json["wind"]` with its return type
- 如何获取CGPDFDictionaryRef键
- 以协议types为关键字的Swift Dictionary
- Swift:'(NSObject,AnyObject)'没有一个名为'subscript'的成员
- 如何在swift中创build和添加Dictionary的值
- iOS 5中的“定义”编辑菜单项的select器
- 在Swift中对NSDictionary对象的NSArray进行sorting
- sorting一个NSMutableDictionary
- iOS / Swift 1.2 NSDictionray / Dictionary上的NSStandardUserDefaults
- 我怎样才能将两个数组合并成一个字典?