lapacino
lapacino

Reputation: 171

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

Upvotes: 2

Views: 2190

Answers (2)

mspasov
mspasov

Reputation: 451

If you really want to force gregorian calendar use the following NSCalendar initializer:

        let gregorianCalendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)

Upvotes: 2

Mobile Developer
Mobile Developer

Reputation: 5760

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

Upvotes: 2

Related Questions