9

My code:

 let calendar = NSCalendar.currentCalendar()
 let dateComponents = calendar.components(NSCalendarUnit.YearCalendarUnit | NSCalendarUnit.MonthCalendarUnit | NSCalendarUnit.DayCalendarUnit, fromDate: self)

My error:

'YearCalendarUnit' was deprecated in OS X version 10.10: Use NSCalendarUnitYear instead
'MonthCalendarUnit' was deprecated in OS X version 10.10: Use NSCalendarUnitMonth instead
'DayCalendarUnit' was deprecated in OS X version 10.10: Use NSCalendarUnitDay instead

But I can't type NSCalendarUnitYear as well as (NSCalendarUnitMonth/ NSCalendarUnitDay) What is wrong?

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
Huynh Inc
  • 2,010
  • 1
  • 25
  • 42

1 Answers1

14

Use let dateComponents = calendar.components(.CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitDay, fromDate: self)

Edit:
This is because the error messages are for Objective-C, not adapted for swift.

Edit 2: see Will's comment for Swift 2 and on.

Schemetrical
  • 5,506
  • 2
  • 26
  • 43
  • .CalendarUnitYear is also valid in the context of an NSCalendarUnit – Robert Wagstaff Apr 16 '15 at 23:28
  • 5
    As of Swift 2, you use the `OptionSetType` syntax. For this same code, it'd look like `calendar.components([.Year, .Month, .Day] , fromDate: self)` See http://stackoverflow.com/questions/24066170/how-to-create-ns-options-style-bitmask-enumerations-in-swift for more – Will Oct 14 '15 at 02:54