Swift 2.0 'Element.Protocol' 没有名为 'Hour'、NSCalendar.components 的成员

Swift 2.0 'Element.Protocol' does not have a member named 'Hour', NSCalendar.components

我遇到了 Swift 2.0 和 NSDateComponents 的问题。

let calendar = NSCalendar.currentCalendar()
let components: NSDateComponents = calendar.components([.Hour,
    .Minute, .Second], fromDate: self.dateTime , toDate: NSDate(), options: nil)

我知道从 Swift 2.0 .Hour | .Minute 不能再使用了,所以尝试使用 OptionSetType 的新方法。

喜欢这里的回答:

但是没有用。 XCode 给我以下错误: 'Element.Protocol' 没有名为 'Hour'

的成员

有人知道如何修复它吗?

尽管有错误消息,Xcode 似乎不喜欢您的 options: nil。请尝试使用 []

calendar.components([.Hour, .Minute, .Second], fromDate: NSDate(),
    toDate: NSDate(), options: nil) // error

calendar.components([.Hour], fromDate: NSDate(),
    toDate: NSDate(), options: []) // no error