gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:55:08 24 4
gpt4 key购买 nike

谁知道我该如何解决这个问题?我收到此错误:

¨cannot convert value of type 'NSDate' to expected argument type 'NSDateComponents'¨ on line:

错误发生在以下行:

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

最佳答案

在您原来的问题中,您调用 dateFromComponents , 它转换 NSDateComponentsNSDate , 但提供了一个 competitionDate , 这已经是一个 NSDate .编译器只是简单地通知您这个错误。

--

顺便说一句,您填充一个 NSDateComponents.Hour , .Minute , .Second , 和 .Nanosecond来自NSDate , 但随后继续尝试保存对 components.year 的引用和 components.monthcomponents.day即使您没有指定这些组件应包含在 NSDateComponents 中.

--

下面你说:

It's a Countdown app and i had it running until i wanted a DatePicker to choose the date i am counting down. The competitionDate is the due date

如果这确实是您想要的,那么也许 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)
}
}

关于Swift:无法将类型 'NSDate' 的值转换为预期的参数类型 'NSDateComponents',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33859964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com