這是我第一次在論壇上詢問,因爲我對編程相當陌生。For循環以隨機順序追加陣列
我正在嘗試使用BEMSimpleLineGraph在swift中創建歷史匯率圖,並使用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))")
}
}
}
}
}
CONSOLE數組:
[]
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。我正在使用多個已修改的網址,例如api.fixer.io/2017-03-01?base=GB。
謝謝!
你想要什麼?你想對數據進行排序嗎 –
你可以同時發起一系列的http請求,但不能保證他們會按照他們被解僱的順序完成。 –
嗨,你究竟想要達到什麼樣的目標和輸出你爲什麼認爲這是錯誤的輸出。 如果您可以發佈您從http://api.fixer.io/生成的網址,這將會很有幫助。 –