Create a struct for example
struct Rate { let currency : String let date : Date var value : Double }Create an array
var historicalRates = [Rate]().In the
forloopcalculate the date viawith the
CalendarandDateComponentsAPI, your way gets in trouble on overflow to the next month. For examplelet calendar = Calendar.current // set the date to noon to avoid daylight saving changes at midnight in a few countries let today = calendar.date(bySettingHour: 12, minute: 0, second: 0, of: Date())! let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd" for dayOffset in 0...7 { let currentDate = calendar.date(byAdding: .day, value: dayOffset, to: today)! let currentDateAsString = formatter.string(from: currentDate) print(currentDate, currentDateAsString) }create a
Datefrom the current date.create a
Rateinstance with actual date and name, add it to thehistoricalRatesand pass it to the asynchronous task.Assign the rate
valuein the completion block.Use
DispatchGroupto get notified when the loop is finished.Finally sort
historicalRatesbydate.
Create a struct for example
struct Rate { let currency : String let date : Date var value : Double }Create an array
var historicalRates = [Rate]().In the
forloopcalculate the date via
CalendarandDateComponents, your way gets in trouble on overflow to the next month.create a
Datefrom the current date.create a
Rateinstance with actual date and name, add it to thehistoricalRatesand pass it to the asynchronous task.Assign the rate
valuein the completion block.Use
DispatchGroupto get notified when the loop is finished.Finally sort
historicalRatesbydate.
Create a struct for example
struct Rate { let currency : String let date : Date var value : Double }Create an array
var historicalRates = [Rate]().In the
forloopcalculate the date with the
CalendarAPI, your way gets in trouble on overflow to the next month. For examplelet calendar = Calendar.current // set the date to noon to avoid daylight saving changes at midnight in a few countries let today = calendar.date(bySettingHour: 12, minute: 0, second: 0, of: Date())! let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd" for dayOffset in 0...7 { let currentDate = calendar.date(byAdding: .day, value: dayOffset, to: today)! let currentDateAsString = formatter.string(from: currentDate) print(currentDate, currentDateAsString) }create a
Datefrom the current date.create a
Rateinstance with actual date and name, add it to thehistoricalRatesand pass it to the asynchronous task.Assign the rate
valuein the completion block.Use
DispatchGroupto get notified when the loop is finished.Finally sort
historicalRatesbydate.
Create a struct for example
struct Rate { let currency : String let date : Date var value : Double }Create an array
var historicalRates = [Rate]().In the
forloopcalculate the date via
CalendarandDateComponents, your way gets in trouble on overflow to the next month.create a
Datefrom the current date.create a
Rateinstance with actual date and name, add it to thehistoricalRatesand pass it to the asynchronous task.Assign the rate
valuein the completion block.Use
DispatchGroupto get notified when the loop is finished.Finally sort
historicalRatesbydate.