Search code examples
iosswiftios8nscalendargregorian-calendar

how i can fix this date issues for ios8


i don't know how to write the right code to fix my date issue

class Date {

class func from(#year:Int, month:Int, day:Int) -> NSDate{

    var components = NSDateComponents()
    components.year = year
    components.month = month
    components.day = day

  let gregorianCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
    let date = gregorianCalendar?.dateFromComponents(components)

    return date!
}

this is the error message 'NSGregorianCalendar' was deprecated in iOS version 8.0: Use NSCalendarIdentifierGregorian instead

but when i replace the next line didn't work


Solution

  • just use:

    let calendar = NSCalendar.currentCalendar()
    

    It will return you gregorian calendar (depending on locale settings).

    Another option is to define constants and use GregorianCalendar as a calendar name :

    #ifdef __IPHONE_8_0
        #define GregorianCalendar NSCalendarIdentifierGregorian
    #else
        #define GregorianCalendar NSGregorianCalendar
    #endif