2016-10-24 22 views
0

返回错误消息:类型'Any'在最后2行代码中没有下标成员。我不能在Swift 3中为NSArray解决这个问题:任何想法?如何将下标成员转换为NSArray Swift 3

let weatherDictionary: NSDictionary = (try! JSONSerialization.jsonObject(with: dataObject!, options: [])) as! NSDictionary 
_ = try JSONSerialization.jsonObject(with: dataObject!, options: .allowFragments) as! [String:Any] 

然后

struct Weekly{ 
    var dayZeroTemperatureMax: Int 
    var dayZeroTemperatureMin: Int 

    init (weatherDictionary: NSDictionary) { 

    let weeklyWeather = weatherDictionary["daily"] as! NSDictionary 
    let weeklyForcast = weeklyWeather["data"] as! NSArray 

    //DAY ZERO 

    dayZeroTemperatureMax = weeklyForcast[0]["temperatureMax"] as! Int 
    dayZeroTemperatureMin = weeklyForcast[0]["temperatureMin"] as! Int 
    } 
    } 
+0

let weatherDictionary:NSDictionary =(try!JSONSerialization.jsonObject(with:dataObject !, options:[]))as! NSDictionary – Bux

+0

让weeklyWeather = Weekly(weatherDictionary:weatherDictionary) – Bux

+0

!! http://json.org/example.html请点击这里。希望你了解什么是JSON! –

回答

1

试着改变你的代码如下:

let weeklyWeather = weatherDictionary["daily"] as! [String: AnyObject] 
let weeklyForcast = weeklyWeather["data"] as! [[String: Any]] 

在这同一项目中,您可能需要更改这个太(在WeatherAlerts):

init (weatherDictionary: [String: Any]) { 

    if let weatheralerts = (weatherDictionary["alerts"] as? [[String: Any]]) { 

     userAlert = weatheralerts[0]["title"] as! String 

    } else { 
     userAlert = "" 
    } 
} 

我真的不知道有理由改变这一点,如果有人有解释请在这里评论。

但在相同的project我有一个“致命错误:意外地发现零,而解包可选值”错误here,你有什么想法吗? URLSessionDownloadTask有什么变化?

+0

非常感谢!这非常出色。我也没有解释,但它的工作原理! – Bux