Xcode 8和Swift 3 NSCalendar

The*_*per 16 ios swift swift2 swift3

我在Swift中实现了我的watch项目,现在我因为Xcode 8而迁移到Swift 3.我让Xcode 8将源代码更改为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)
Run Code Online (Sandbox Code Playgroud)

Xcode在这些行中给出了错误,我无法理解这个问题.

错误:上下文类型"日历"不能与数组文字一起使用

错误:参数标签'(标识符:)'与任何可用的重载都不匹配

错误:无法将"日历"类型的值转换为预期的参数类型"NSCalendar.Unit"

vad*_*ian 34

首先,NSCalendarUnitSwift 2和Calendar.ComponentSwift 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)
Run Code Online (Sandbox Code Playgroud)