Xcode 8 和 Swift 3

Xcode 8 and Swift 3 NSCalendar

我在Swift实现了我的手表项目,现在由于Xcode8,我正在迁移到Swift3。我让Xcode8将源代码更改为Swift 3. 但是代码有错误,我想不通

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 在这些行中给出了错误,我无法理解这个问题。

ERROR: Contextual type ' Calendar' cannot be used with array literal

ERROR: Argument labels '(identifier:)' do not match any available overloads

ERROR: Cannot convert value of type 'Calendar' to expected argument type 'NSCalendar.Unit'

将 unitFlags 的类型更改为 Set

首先,NSCalendarUnit in Swift 2 和 Calendar.Component in Swift 3 都不包含组件 firstWeekdaymonthSymbols

在 Swift 3 中,您的代码等效于

let unitFlags = Set<Calendar.Component>([.hour, .year, .minute])
var calendar = Calendar.current
calendar.timeZone = TimeZone(identifier: "UTC")!
let components = calendar.dateComponents(unitFlags, from: reservationDate)