Create a Date, one day into the future, use a formatter to turn it into a string, then serialize the string as Data. Now reverse the process, read the data, turn it into a string, and use a date formatter to turn it back into a date.

Solution

  import Foundation

  func solution() {
    //Create a `Date`, one day into the future,
    let gregorianCalendar:Calendar = Calendar(identifier: .gregorian)
    var advanceByADay = DateComponents()
    advanceByADay.day = 1
    guard let tomorrow = gregorianCalendar.date(byAdding: advanceByADay, to: Date())
      else { return }

    //use a formatter to turn it into a string
    let dateFormatter:ISO8601DateFormatter = ISO8601DateFormatter()
    let tomorrowString:String = dateFormatter.string(from: tomorrow)
    guard let tomorrowData:Data = tomorrowString.data(using: .utf8)
      else { return }

    //Now reverse the process.
    //turn it into a string
    guard let newString:String = String(data:tomorrowData, encoding:.utf8)
      else { return }

    //use a date formatter to turn it back into a date.
    guard let newDate:Date = dateFormatter.date(from: newString)
      else { return }
    print(newDate)
  }