gpt4 book ai didi

ios - AlamofireObjectMapper 结果为零

转载 作者:行者123 更新时间:2023-11-30 12:15:35 24 4
gpt4 key购买 nike

所以我正在使用天气 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 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. 如何将其放入数组中以使其成为图表并继续我的工作?

提前致谢。

最佳答案

您的代码的问题是 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 }
}

}
}

编辑:删除可编码答案

关于ios - AlamofireObjectMapper 结果为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45472128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com