Sebuah bekerja pelaksanaan Swift 3 + berdasarkan highmaintenance ini jawaban dan vikingosegundo ini komentar. Ekstensi Tanggal ini juga memiliki opsi tambahan untuk mengubah tahun, bulan, dan waktu:
extension Date {
/// Returns a Date with the specified amount of components added to the one it is called with
func add(years: Int = 0, months: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Int = 0) -> Date? {
let components = DateComponents(year: years, month: months, day: days, hour: hours, minute: minutes, second: seconds)
return Calendar.current.date(byAdding: components, to: self)
}
/// Returns a Date with the specified amount of components subtracted from the one it is called with
func subtract(years: Int = 0, months: Int = 0, days: Int = 0, hours: Int = 0, minutes: Int = 0, seconds: Int = 0) -> Date? {
return add(years: -years, months: -months, days: -days, hours: -hours, minutes: -minutes, seconds: -seconds)
}
}
Penggunaan untuk hanya menambah satu hari seperti yang diminta OP akan menjadi:
let today = Date() // date is then today for this example
let tomorrow = today.add(days: 1)