“yearcalendar arunit”在OS X版本10.10中被弃用:使用NSCalendarUnitYear

时间:2020-12-14 11:50:47

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?

但是我不能输入NSCalendarUnitYear和(NSCalendarUnitMonth/ NSCalendarUnitDay)有什么问题吗?

1 个解决方案

#1


14  

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

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

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

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

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

编辑2:参见Will对Swift 2的评论。

#1


14  

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

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

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

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

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

编辑2:参见Will对Swift 2的评论。