Я бы предложил вам использовать вот так:
Ниже кода вы получите даты всех воскресений с 2016-2018 ,
let cal = Calendar.current
// Get the date of 2 years ago today
let stopDate = cal.date(byAdding: .year, value: -2, to: Date())!
// We want to find dates that match on Sundays at midnight local time
var comps = DateComponents()
comps.weekday = 1 // Sunday
// Enumerate all of the dates
cal.enumerateDates(startingAfter: Date(), matching: comps, matchingPolicy: .previousTimePreservingSmallerComponents, repeatedTimePolicy: .first, direction: .backward) { (date, match, stop) in
if let date = date {
if date < stopDate {
stop = true // We've reached the end, exit the loop
} else {
print("\(date)") // do what you need with the date
}
}
}
Логика проста. Сделайте массив из этого в вашем необходимом dateFormat
и покажите, что в pickerView
, который в конечном итоге будет работать как datePicker
для вас.
Надеюсь, это поможет.