Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Futhark

macrumors 65816
Original poster
Jun 12, 2011
1,238
179
Northern Ireland
Hi all, I'm having issues with some code in Xcode 8 that obviously worked in Xcode 7 under Swift 2.3 here are both versions of the code, Swift 2.3 code commented out and the now not working Swift 3 Code.

The error message I get is Type 'Set<Calendar.Component> has no member 'day'

Code:
/* extension NSDate {
    func dayOfWeek() -> Int? {
        guard
            let calender: NSCalendar = NSCalendar.currentCalendar(),
            let component: NSDateComponents = calender.components(.Weekday, fromDate: self) else { return nil }
        return component.weekday
    }
} */

extension Date {
    func dayOfWeek() -> Int? {
        guard
            let calendar: Calendar = Calendar.current,
            let component: DateComponents = calendar.dateComponents(.day, from: self) else { return nil }
        return component.weekday
    }
}

Any help much appreciated
 
Swift 3 should fly like this, just take whatever component you need from the calendar.
Code:
let date =Date()
let calendar =Calendar.current()
let components = calendar.components([.hour,.minute,.second,.nanosecond],from: date)
 
Last edited by a moderator:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.