2016-08-12 32 views
13

Tôi đã triển khai dự án đồng hồ của mình trong Swift và bây giờ tôi đang chuyển sang Swift 3 vì Xcode 8. Tôi để Xcode 8 thay đổi mã nguồn thành Swift 3. Tuy nhiên, có lỗi trong mã và Tôi không thể hình dung nó ra.Xcode 8 và 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 cho lỗi trong các dòng này và tôi không thể hiểu được vấn đề.

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'

Trả lời

29

Trước hết, không phải NSCalendarUnit trong Swift 2 lẫn Calendar.Component trong Swift 3 chứa các thành phần firstWeekdaymonthSymbols.

Trong Swift 3 tương đương với mã của bạn là

let unitFlags = Set<Calendar.Component>([.hour, .year, .minute]) 
var calendar = Calendar.current 
calendar.timeZone = TimeZone(identifier: "UTC")! 
let components = calendar.dateComponents(unitFlags, from: reservationDate) 
Các vấn đề liên quan