Skip to main content
added 646 characters in body
Source Link
vadian
  • 286.3k
  • 32
  • 378
  • 379
  • Create a struct for example

     struct Rate { let currency : String let date : Date var value : Double } 
  • Create an array var historicalRates = [Rate]().

  • In the for loop

  • calculate the date viawith the Calendar and DateComponents API, your way gets in trouble on overflow to the next month. For example

     let 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 Date from the current date.

  • create a Rate instance with actual date and name, add it to the historicalRates and pass it to the asynchronous task.

  • Assign the rate value in the completion block.

  • Use DispatchGroup to get notified when the loop is finished.

  • Finally sort historicalRates by date.

  • Create a struct for example

     struct Rate { let currency : String let date : Date var value : Double } 
  • Create an array var historicalRates = [Rate]().

  • In the for loop

  • calculate the date via Calendar and DateComponents, your way gets in trouble on overflow to the next month.

  • create a Date from the current date.

  • create a Rate instance with actual date and name, add it to the historicalRates and pass it to the asynchronous task.

  • Assign the rate value in the completion block.

  • Use DispatchGroup to get notified when the loop is finished.

  • Finally sort historicalRates by date.

  • Create a struct for example

     struct Rate { let currency : String let date : Date var value : Double } 
  • Create an array var historicalRates = [Rate]().

  • In the for loop

  • calculate the date with the Calendar API, your way gets in trouble on overflow to the next month. For example

     let 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 Date from the current date.

  • create a Rate instance with actual date and name, add it to the historicalRates and pass it to the asynchronous task.

  • Assign the rate value in the completion block.

  • Use DispatchGroup to get notified when the loop is finished.

  • Finally sort historicalRates by date.

Source Link
vadian
  • 286.3k
  • 32
  • 378
  • 379

  • Create a struct for example

     struct Rate { let currency : String let date : Date var value : Double } 
  • Create an array var historicalRates = [Rate]().

  • In the for loop

  • calculate the date via Calendar and DateComponents, your way gets in trouble on overflow to the next month.

  • create a Date from the current date.

  • create a Rate instance with actual date and name, add it to the historicalRates and pass it to the asynchronous task.

  • Assign the rate value in the completion block.

  • Use DispatchGroup to get notified when the loop is finished.

  • Finally sort historicalRates by date.