Swift:无法将'NSDate'类型的值转换为预期的参数类型'NSDateComponents'

时间:2015-11-22 20:45:41

标签: swift nsdate nscalendar nsdatecomponents

任何人都知道如何解决这个问题?我收到这个错误:

  

- 无法将'NSDate'类型的值转换为预期参数类型'NSDateComponents'¨在线:

错误发生在以下行:

let competitionDay = userCalendar.dateFromComponents(competitionDate)!

这是代码的更完整摘录:

func Date() {               

    // Here we set the current date

    let date = NSDate()
    let calendar = NSCalendar.currentCalendar()
    let components = calendar.components([.Hour, .Minute, .Second, .Nanosecond], fromDate: date)
    let hour = components.hour
    let minutes = components.minute
    let month = components.month
    let year = components.year
    let day = components.day


    let currentDate = calendar.dateFromComponents(components)



    // here we set the due date. When the timer is supposed to finish
    // final Calendar value

        let userCalendar = NSCalendar.currentCalendar()


        let competitionDate:NSDate = myDatePicker.date


        competitionDate.timeIntervalSinceNow


        let competitionDay = userCalendar.dateFromComponents(competitionDate)!


    // Here we compare the two dates
    competitionDay.timeIntervalSinceDate(currentDate!)


    let dayCalendarUnit = calendar.components([NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Minute], fromDate: date)


    //here we change the seconds to hours,minutes and days


    let competitionDayDifference = calendar.components([.Day, .Hour, .Minute],
            fromDate: currentDate!, toDate: competitionDay, options: NSCalendarOptions())

    //finally, here we set the variable to our remaining time
    let daysLeft = competitionDayDifference.day
    let hoursLeft = competitionDayDifference.hour
    let minutesLeft = competitionDayDifference.minute

1 个答案:

答案 0 :(得分:1)

在原始问题中,您拨打了dateFromComponents,将NSDateComponents转换为NSDate,但提供了competitionDate,已经是NSDate }。编译器只是告诉你这个错误。

-

顺便说一句,您使用NSDateComponents中的.Hour.Minute.Second.Nanosecond来填充NSDate,但是然后继续尝试保存对components.yearcomponents.month以及components.day的引用,即使您没有指定这些组件应包含在NSDateComponents中。< / p>

-

在下面你说:

  

它是一个倒计时应用程序,我一直在运行,直到我想让DatePicker选择我倒计时的日期。比赛日期是截止日期

如果这确实是你想要的,那么根本不需要NSDateComponents。我可能只是使用NSDateComponentsFormatter方法stringFromDate:toDate:向用户显示两个NSDate对象之间的时间量的精确字符串表示。

例如:

class ViewController: UIViewController {

    @IBOutlet weak var datePicker: UIDatePicker!
    @IBOutlet weak var label: UILabel!

    weak var timer: NSTimer?

    let formatter: NSDateComponentsFormatter = {
        let _formatter = NSDateComponentsFormatter()
        _formatter.allowedUnits = [.Year, .Month, .Day, .Hour, .Minute, .Second]
        _formatter.unitsStyle = .Full

        return _formatter
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        datePicker.date = NSDate().dateByAddingTimeInterval(1000) // initialize it to whatever you want
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "didFireTimer:", userInfo: nil, repeats: true)
    }

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)

        timer?.invalidate()
    }

    func didFireTimer(timer: NSTimer) {
        label.text = formatter.stringFromDate(NSDate(), toDate: datePicker.date)
    }
}