Upgrading app for iOS 10 Problem

Discussion in 'iOS Programming' started by Futhark, Aug 6, 2016.

Tags:
  1. Futhark macrumors 65816

    Futhark

    Joined:
    Jun 12, 2011
    Location:
    Northern Ireland
    #1
    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
     
  2. Crichton333, Aug 8, 2016
    Last edited by a moderator: Aug 8, 2016

    Crichton333 macrumors 6502

    Crichton333

    Joined:
    May 4, 2014
    #2
    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)
     

Share This Page