Xcode 8 and Swift 3 NSCalendar

I implemented my watch project in Swift, and now I am migrating to Swift 3 because of Xcode 8. I allow Xcode 8 to change the source code to Swift 3. However, there are errors in the code, and I cannot figure it out.

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 gives an error in these lines, and I cannot understand the problem.

ERROR: Context 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'

+13
ios swift swift2 swift3
source share
2 answers

First of all, neither NSCalendarUnit in Swift 2 nor Calendar.Component in Swift 3 contain the firstWeekday and monthSymbols .

In Swift 3, the equivalent of your code

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

Change type unitFlags to Set

0
source share

All Articles