2016-08-12 119 views
13

我在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'

回答

29

首先,既不NSCalendarUnit在夫特2也不Calendar.Component在夫特3包含的組件firstWeekdaymonthSymbols

在斯威夫特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)