Xcode 7: Can't use CalendarUnitMonth in Swift

2019-04-14 04:19发布

问题:

I am using swift2.

I used this code:

let calendar = NSCalendar.currentCalendar()             
let components = calendar.components(.CalendarUnitMonth | .CalendarUnitDay, fromDate: theDate) 

I am getting this error: Could not find member 'CalendarUnitMonth

回答1:

Check Swift 2 documentation and search for OptionSetType (adopted by NSCalendarUnit). You can use it in this way:

let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Month, .Day], fromDate:NSDate())


标签: swift2 xcode7