在OS X版本10.10中不推荐使用“YearCalendarUnit”:改为使用NSCalendarUnitYear

时间:2015-04-10 07:38:18

标签: ios macos swift

我的代码:

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

我的错误:

'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

但我无法打字 NSCalendarUnitYear以及(NSCalendarUnitMonth / NSCalendarUnitDay) 有什么问题?

1 个答案:

答案 0 :(得分:14)

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

编辑:
这是因为错误消息是针对Objective-C的,不适合swift。

编辑2:请参阅Will对Swift 2及其他人的评论。