【发布时间】:2017-07-31 11:50:26
【问题描述】:
我目前正在尝试使用 BEMSimpleLineGraph 快速创建历史汇率图表,并使用 AlomoFire 从http://fixer.io/ 获取数据。我正在使用 for 循环循环 7 天(只是为了看看我是否可以让它工作),然后将值附加(或其他任何名称)到一个名为 xAxisData 的数组
func updateGraphData(timeInterval: Int){
if selectedCurrency1 != nil && selectedCurrency2 != nil { // checking if both currencies have been selected
self.xAxisData.removeAll() // removing some default values
for i in 1...timeInterval { // don't know exactly if i'm doing this the optimal way?
print("passed")
let date = Date()
let dateComponents = Calendar.current.dateComponents([.month, .day,.year], from: date) //getting the the year(again, just to see if it's working)
historyURL = "http://api.fixer.io/\(dateComponents.year!.description)-03-0\(String(i))?base=\(selectedCurrency1!.rawValue)" //modifying the url to my needs
Alamofire.request(historyURL, method: .get).responseJSON { // requesting data
response in
if response.result.isSuccess{
let json = JSON(response.result.value!)
self.xAxisData.append(json["rates"] [self.selectedCurrency2!.rawValue].doubleValue) // using SwiftyJSON btw to convert, but shouldn't this in theory append in the correct order?
print(json["date"].stringValue) // printing out the date
}
else{
print("Error \(String(describing: response.result.error))")
}
}
}
}
}
控制台:
[]
2017-03-02
2017-03-03
2017-03-01
2017-03-03
2017-03-03
2017-03-06
2017-03-07
[4.5359999999999996, 4.5316000000000001, 4.4739000000000004, 4.5316000000000001, 4.5316000000000001, 4.5133000000000001, 4.4844999999999997]
我知道我犯了一个错误,将货币值设置为双倍,而它可能应该是一个浮点数。如果需要,请随时询问更多信息或以任何其他方式纠正我的问题,因为我只是在努力学习。
我希望输出按时间顺序排列,因此日期为 1,2,3,4,5,6,7 而不是 2,3,1,3,3,6,7。我正在使用多个经过修改的 URL,例如 api.fixer.io/2017-03-01?base=GB。
【问题讨论】:
-
你想要什么??是否要对数据进行排序
-
您同时发起一系列 http 请求,不能保证它们会按照它们被触发的顺序完成。
-
您好,您到底想要实现什么以及您的输出,为什么您认为它是错误的输出。如果您可以发布从 api.fixer.io 生成的 URL,也会很有帮助。
-
我希望输出按时间顺序排列,所以日期是 1,2,3,4,5,6,7 而不是 2,3,1,3,3,6,7 .我正在使用多个经过修改的 URL,例如 api.fixer.io/2017-03-01?base=GB。正如 mag_zbc 所建议的那样,可能的解释是,但我如何实现这一目标
-
@user8288212 试试我的解决方案,它可以同步 web 服务调用并生成不同的 url
标签: ios arrays swift for-loop bemsimplelinegraph