You can get the current week monday's date, and just add n days to it:
Swift 4.1 ? Xcode 9.3 or later
extension Calendar {
static let iso8601 = Calendar(identifier: .iso8601)
}
extension Date {
var cureentWeekMonday: Date {
return Calendar.iso8601.date(from: Calendar.iso8601.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))!
}
var currentWeekdays: [Date] {
return (0...4).compactMap{ Calendar.iso8601.date(byAdding: DateComponents(day: $0), to: cureentWeekMonday) } // for Swift versions earlier than 4.1 use flatMap instead
}
}
let currentWeekdays = Date().currentWeekdays
print(currentWeekdays) // ["Jul 17, 2017, 12:00 AM", "Jul 18, 2017, 12:00 AM", "Jul 19, 2017, 12:00 AM", "Jul 20, 2017, 12:00 AM", "Jul 21, 2017, 12:00 AM"]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…