Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Sean Lee
5,196 PointsGet the day of the week (SWIFT)
hello,
I am trying to get the day of the week starting from today until next week. I assume to start out with NSDate and NSDateFormatter. Can someone direct me in the correct direction on where to start? I found the following codes online but "component.day" in the code below gives me a compiler error.
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitMonth | .CalendarUnitYear | .CalendarUnitDay, fromDate: date)
let hour = components.hour
let minutes = components.minute
let month = components.month
let year = components.year
let day = components.day
Much appreicated! thanks.
1 Answer
Matt Lassiter
Courses Plus Student 10,947 PointsHi there! I am quite new at swift myself, I know this thread is a bit old. But just in case someone was curious, I do have something of a solution. When using the above code, component.day actually spits out an index value of sorts for the day of the week. To properly access and use it, you need to assign the index value to a dy. So perhaps a switch case would be appropriate.
This may not be the best method, but at my current understanding it is working well for me.