I'm currently trying to create a historical exchange rate graph in swift using BEMSimpleLineGraph, and grabbing data from http://fixer.io/ using AlomoFire. I'm using a for-loop to loop through 7 day(just to see if I can get it working) and then appending (or whatever it's called) the values to an array called 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]
I know I made the mistake of making the currency values a double when it probably should've been a float. Feel free to ask for more information if needed or to correct my in any other way, as I'm just trying to learn.
I want the output to be in chronological order, so the date goes 1,2,3,4,5,6,7 instead of 2,3,1,3,3,6,7. I'm using using multiple URLs that are modified, api.fixer.io/2017-03-01?base=GB for example.