【问题标题】:AlamofireObjectMapper Resulting in nilAlamofireObjectMapper 导致 nil
【发布时间】:2017-08-02 22:40:32
【问题描述】:

所以我正在使用天气 API: https://api.darksky.net/forecast/88d117d813f2014a1ce7f3de6a00c720/50.909698,-1.404351

我想要实现的是将每分钟的所有 precipIntensity 放入一个数组中。这样我可以制作一个图表,其中 Y 是 precipIntensity,X 是 array.count(使用这个 API 最多可以计算 61)。

这是我在 ViewController 中的代码:

func downloadWeatherDetails(completed: DownloadComplete) {
    // Alamofire Download
    let currentWeatherURL = URL(string: WeatherURL)!
    Alamofire.request(currentWeatherURL).responseObject { (response: DataResponse<WeatherResponse>) in

        let Forecast = response.result.value
        if let minutelyForecast = Forecast?.minutelyForecast {
            for forecast in minutelyForecast {
                print(forecast.time)
                print(forecast.precipIntensity)
            }
        }

    }
}

而我的Class文件如下:

class WeatherResponse: Mappable {
    var summary: String!
    var minutelyForecast: [Forecast]?

    required init?(map: Map) {

    }

    func mapping(map: Map) {
        summary <- map["minutely.summary"]
        minutelyForecast <- map["minutely.data"]
    }
}

class Forecast: Mappable {
    var time: String!
    var precipIntensity: String!

    required init?(map: Map) {

    }

    func mapping(map: Map) {
        time <- map["time"]
        precipIntensity <- map["precipIntensity"]
    }
}

但是 print(forecast.precipIntensity) 打印出 nil,61 次。

  1. 如何获取此信息?
  2. 我怎样才能将它放入一个数组中以使其成为一个图表并继续我的工作?

提前致谢。

【问题讨论】:

  • 想要/需要使用ObjectMapper 吗?如果您正在考虑迁移到 swift 4,则可以改用 Codable 并删除 ObjectMapper 作为第三方依赖项
  • @nathan 我没听说过 Codable?上一个项目我使用 Object Mapper + Alomofire 从 API 获取数据,所以我认为这仍然是一个不错的方法。
  • 更新了答案并修复了您的问题 + 2 个使用 Codable 的解决方案。如果您想了解有关 Codable/JSONEncoder/JSONDecoder 的更多信息,请查看带有相应标签的其他问题。很容易上手,因为简单的 JSON 结构只需要一行代码

标签: ios json swift alamofire objectmapper


【解决方案1】:

您的代码的问题在于 timeprecipIntensity 不是字符串值,而是数字。

class Forecast: Mappable {
    var time: Int!
    var precipIntensity: Double!
    ...
}

提取降水数据:

func downloadWeatherDetails(completed: DownloadComplete) {
    // Alamofire Download
    let currentWeatherURL = URL(string: WeatherURL)!
    Alamofire.request(currentWeatherURL).responseObject { (response: DataResponse<WeatherResponse>) in

        let Forecast = response.result.value
        if let minutelyForecast = Forecast?.minutelyForecast {
            // Data is already sorted from the API
            // but you could be sure nothing happened while decoding
            // by using `sorted()`
            let minutelyPrecipIntensity = minutelyForecast
                .sorted { $0.time < $1.time }
                .flatMap { $0.precipIntensity }
        }

    }
}

编辑:删除可编码答案

【讨论】:

  • 我不明白这如何帮助我将 API 中的 precipIntensity 信息获取到数组中。
  • minutelyForecast 已经是一个数组,其中包含按时间排序的天气数据(从现在到现在 + 60 分钟)
  • 如果还想只提取precipitationData,可以使用minutelyPrecipIntensity
  • 是的,但是对于minutelyForecast,我只想要precipIntensity 并将它们放入一个新数组中。这样我就可以得到一个包含雨信息的数组,然后我可以用它制作一个图表。
  • minutelyPrecipIntensity (not minutelyForecast) 是一个只有 precipitationData 的数组,如您的第二个问题所述
猜你喜欢
  • 2021-09-13
  • 2019-06-25
  • 1970-01-01
  • 2016-04-28
  • 1970-01-01
  • 1970-01-01
  • 2018-02-03
  • 2018-01-12
  • 1970-01-01
相关资源
最近更新 更多