Swift4 - JSON 数组的可解码问题(无法下标值)

Swift4 - JSON Decodable issue with array (Cannot subscript a value)

我正在使用 Swift 4 Decodable 解析天气 JSON,但数组数据存在问题。我想我的模型可能有点不对(因为有一堆可解码的结构),请帮忙。

JSON 天气数据 API:

{"coord":{"lon":-43.21,"lat":-22.9},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],
"base":"stations","main":{"temp":299.37,"pressure":1008,"humidity":51,"temp_min":296.15,"temp_max":302.15},
"visibility":10000,"wind":{"speed":4.1,"deg":320},"clouds":{"all":0},
"dt":1510909200,"sys":{"type":1,"id":4565,"message":0.0023,"country":"BR","sunrise":1510905615,
"sunset":1510953350},"id":3451190,"name":"Rio de Janeiro","cod":200}

型号:

struct WeatherData:Decodable {
let coord:CoordDict?
let weather:[WeatherArr]?
let base: String?
let main:MainDict?
let visibility:Int?
let wind:WindDict?
let clouds:CloudsDict?
let dt:Int?
let sys:SysDict?
let id:Int?
let name:String?
let cod:Int?}

struct CoordDict:Decodable {
let lon: Double?
let lat: Double?}

struct WeatherArr:Decodable {
let id: Int?
let main:String?
let description:String?
let icon: String?}

struct MainDict:Decodable {
let temp:Double?
let pressure:Double?
let humidity:Int?
let temp_min:Double?
let temp_max:Double?
let sea_level:Double?
let grnd_level:Double?}

struct WindDict:Decodable {
let speed:Double?
let deg:Double?}

struct CloudsDict:Decodable {
let all: Int?}

struct SysDict:Decodable {
let type:Int?
let id:Int?
let message:Double?
let country:String?
let sunrise:Int?
let sunset:Int?}

使用JSON解码器解析JSON数据:

    let jsonUrlString = ("https://api.openweathermap.org/data/2.5/weather?lat=\(lat!)&lon=\(lon!)&APPID=\(apikey)")

    guard let url = URL(string: jsonUrlString) else { return }

    URLSession.shared.dataTask(with: url) { (data, response, err) in

        //check err
        if err != nil {
            print("Error:\(String(describing: err))")
        }

        guard let data = data else { return }
        //do stuff

        do {

            let weatherData = try JSONDecoder().decode(WeatherData.self, from: data)

            print(weatherData.name, weatherData.weather)

   //Here i can't parse the weather Array, and get xcode error

     if let wArr = weatherData.weather! as? Array<AnyObject> {
                if let weatherIcon = wArr["icon"] {
                  icon = weatherIcon
                    }
                }
        } catch let jsonErr {
            print("Error:\(jsonErr)")
        }

        }.resume()//URLSession

错误消息:无法使用 'String' 类型的索引为 'Array' 类型的值添加下标。

我该如何解决这个问题?我如何解析天气数组元素?

WeatherData 结构中声明 weather 为非可选。总有天气

let weather : [WeatherArr]

如果数组和图标得到第一项

if let currentWeather = weatherData.weather.first {
   if let weatherIcon = currentWeather.icon  {
      print(weatherIcon)
   }
}