3

I start out with:

var weekdays = ["Sunday", "Friday", "Tuesday"]

I need it changed to:

weekdays = ["Sunday", "Tuesday", "Friday"]

I've converted them to Ints:

var days = [Int]()

for day in weekdays {

    let formatterWeekday = NSDateFormatter()

    formatterWeekday.dateFormat = "e"

    let weekday = formatterWeekday.dateFromString(day)

    let weekdayString = formatterWeekday.stringFromDate(weekday!)

    let dayInt = Int(weekdayString)

    days.append(dayInt!)

}

days.sortInPlace()

Which then gives me:

days = [1,3,6]

So then how do I convert Ints to weekday strings?

1 Answer 1

4

Have you considered the simple approach?

Updated with @Martin R input

let dateFormatter = NSDateFormatter() 
// dateFormatter.weekdaySymbols = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]


for dayIndex in days
{    
    print(dateFormatter.weekdaySymbols[dayIndex])
}
Sign up to request clarification or add additional context in comments.

2 Comments

Note that NSCalendar has a property weekdaySymbols which gives you the (localized) weekday names.
@Russell be aware that weekdaySymbols are locale dependant. You should make sure to add NSLocale(localeIdentifier: "en_US_POSIX")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.