Xcode 8和Swift 3 NSCalendar
问题描述:
我在Swift中实现了我的监视项目,现在我正在迁移到Swift 3,因为Xcode 8。我让Xcode 8将源代码更改为Swift 3.但是,代码中存在错误,我想不明白。Xcode 8和Swift 3 NSCalendar
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'
答
首先,既不NSCalendarUnit
在夫特2也不Calendar.Component
在夫特3包含的组件firstWeekday
和monthSymbols
。
在斯威夫特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)
答
变化unitFlags的类型设置