I try to get days of week from calendar and get the value from it so I can store in object, but I get confused how can I transform the array so I can save in struct. This is how I can get the days of week
let calendar = Calendar.current
let dayOfWeek = calendar.component(.weekday, from: dateInWeek)
let weekdays = calendar.range(of: .weekday, in: .weekOfYear, for: dateInWeek)!
let days = (weekdays.lowerBound ..< weekdays.upperBound)
.compactMap { calendar.date(byAdding: .day, value: $0 - dayOfWeek, to: dateInWeek) }
and then I format it to get the day and the date number
let formatter = DateFormatter()
formatter.dateFormat = "E d"
This is the output of my date
let strings = days.map { formatter.string(from: $0) }
print(strings)
["Sun 27", "Mon 28", "Tue 1", "Wed 2", "Thu 3", "Fri 4", "Sat 5"]
This is the struct how I want to store separately from day and date number
struct Week {
let dayName: String
let dayDate: String
}
I already try this method but the days turn into two dimensional array
for week in strings {
let weeks = week.components(separatedBy: " ")
print(weeks)
}
How can I turn the two dimensional array so I can store in struct?
dayNametoSunanddayDateto27? Btw Do you really need to store day as String? Do you need the weekday name localized or always English?calendar.range(of: .weekday, in: .weekOfYear, for: dateInWeek)!will always return1..<8