参数标签'(_:,fromDate:,toDate:,options:)'不匹配任何可用的重载

时间:2018-04-05 05:50:33

标签: ios swift swift4

        var toTimePVC: UIDatePicker!
        var fromPVC: UIDatePicker!
        let unit:NSCalendar.Unit = .minute
        let cal = NSCalendar.current
        let minutesComponents = cal.components(unit, fromDate:fromPVC.date, toDate:  toTimePVC.date, options: NSCalendar.Options.MatchStrictly)
        let startingTime = cal.components(unit, fromDate: date, toDate: fromPVC.date, options: NSCalendar.Options.MatchStrictly

我在Swift 4中收到此错误。如何解决此问题。

enter image description here

1 个答案:

答案 0 :(得分:0)

自Swift 3以来,Objective-C方法components:fromDate:toDate:options:在Swift中导入为components(_:from:to:options:)

但是按照上面的签名重写你的代码,我收到了这个错误:

let minutesComponents = cal.components(unit, from: fromPVC.date, to: toTimePVC.date, options: NSCalendar.Options.matchStrictly)
'components(_:from:to:options:)' has been renamed to 'dateComponents(_:from:to:)'

使用Calendar代替NSCalendarCalendar.dateComponents(_:from:to:)

    let components: Set<Calendar.Component> = [.minute]
    let cal = Calendar.current
    let minutesComponents = cal.dateComponents(components, from: fromPVC.date, to: toTimePVC.date)