【问题标题】:Swift 2.0 'Element.Protocol' does not have a member named 'Hour', NSCalendar.components [duplicate]Swift 2.0“Element.Protocol”没有名为“Hour”的成员,NSCalendar.components [重复]
【发布时间】:2015-07-17 18:55:45
【问题描述】:

我遇到了 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 7: Can't use CalendarUnitMonth in Swift 2.0, Xcode 7

但它不起作用。 XCode 给我以下错误: “Element.Protocol”没有名为“Hour”的成员

有没有人知道如何解决它?

【问题讨论】:

    标签: ios xcode swift swift2


    【解决方案1】:

    尽管有错误消息,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
    

    【讨论】:

    • 谢谢,我尝试了一切,因为我认为问题出在第一个参数上.. 没想到最后一个!现在一切正常!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多