Xcode 8和Swift 3 NSCalendar [英] Xcode 8 and Swift 3 NSCalendar
问题描述
我在Swift中实现了我的watch项目,由于Xcode 8,我现在迁移到Swift3.我让Xcode 8将源代码更改为Swift3.但是,代码中有错误,我无法弄清楚
I implemented my watch project in Swift and now I am migrating to Swift 3 because of Xcode 8. I let Xcode 8 change the source code to Swift 3. However, there are errors in the code and I can not figure it out.
let unitFlags: Calendar = [.hour, .firstWeekday, .monthSymbols, .year, .minute, .firstWeekday]
var calendar = NSCalendar.current
calendar.timeZone = NSTimeZone(identifier: "UTC")!
let components = (calendar as NSCalendar).components(unitFlags, from: reservationDate)
Xcode在这些行中给出了错误,我无法理解问题.
Xcode gives error in these lines and I can not understand the problem.
错误:上下文类型日历"不能与数组文字一起使用
ERROR: Contextual type ' Calendar' cannot be used with array literal
错误:参数标签'(identifier :)'与任何可用标签都不匹配 超载
ERROR: Argument labels '(identifier:)' do not match any available overloads
错误:无法将日历"类型的值转换为预期参数 键入"NSCalendar.Unit"
ERROR: Cannot convert value of type 'Calendar' to expected argument type 'NSCalendar.Unit'
推荐答案
首先,Swift 2中的NSCalendarUnit
和Swift 3中的Calendar.Component
都不包含组件firstWeekday
和monthSymbols
.
First of all, neither NSCalendarUnit
in Swift 2 nor Calendar.Component
in Swift 3 contain the components firstWeekday
and monthSymbols
.
在Swift 3中,您的代码等同于
In Swift 3 the equivalent of your code is
let unitFlags = Set<Calendar.Component>([.hour, .year, .minute])
var calendar = Calendar.current
calendar.timeZone = TimeZone(identifier: "UTC")!
let components = calendar.dateComponents(unitFlags, from: reservationDate)
这篇关于Xcode 8和Swift 3 NSCalendar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!