1

I currently have an array of weekdays that represent integers which I will trigger a notification on that weekday

let weekdays:[Int] = [1,4,7]

  • in dateComponents, weekdays represent a number so for example - integer 1 represents Sunday and integer 7 represent Saturday - I was wondering how can I transfer those integers to a weekday string - for example in the array weekdays I have [1,4,7] - I want to then print out [Sunday, Wednesday, Saturday] is there a way to do that? any help would be great.
  • 2
    Check out the documentation for Calendar, it has a property `weekdaySymbols` which contains the name of the week days. Note though that the first day of the week (another property) depends on the users locale so maybe you need to adjustor that. – Joakim Danielson Jan 21 '22 at 14:46
  • 2
    `weekdays.map { Calendar.current.weekdaySymbols[$0 - 1] }` might be a working solution. `Calendar.current` might be "correct", you can use another calendar, but keep in mind local, that could have not the expected results (like out of bounds, others days... – Larme Jan 21 '22 at 14:47

1 Answers1

0

Assuming you are ok with 1 = Sunday and 7 = Saturday, maybe something like this can be done:

First sort the array

weekdays = weekdays.sorted()

Then iterate through it

for i in weekdays {
    let weekdayString = Calendar.current.weekdaySymbols[i - 1]
    print(weekdayString)
}

Depending on your needs you may wish to use:

.weekdaySymbols
.shortWeekdaySymbols
.veryShortMonthSymbols

or

.standaloneWeekdaySymbols
.shortStandaloneWeekdaySymbols
.veryShortStandaloneMonthSymbols 
grep
  • 566
  • 5
  • 20