我在Swift中实现了我的watch项目,由于Xcode 8,我现在迁移到Swift3。我让Xcode 8将源代码更改为Swift3。但是,代码中有错误,我无法弄清楚。
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在这些行中给出了错误,我无法理解问题。
错误:上下文类型“日历”不能与数组文字一起使用
错误:参数标签'(identifier :)'与任何可用的重载都不匹配
错误:无法将类型“日历”的值转换为预期的参数类型“ NSCalendar.Unit”
首先,NSCalendarUnit
在Swift 2和Calendar.Component
Swift 3中都没有包含firstWeekday
和monthSymbols
。
在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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句