Xcode 7: Can't use CalendarUnitMonth in Swift

2019-04-14 04:18发布

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

标签: swift2 xcode7
1条回答
▲ chillily
2楼-- · 2019-04-14 04:37

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())
查看更多
登录 后发表回答