Swift: 无法将类型 'NSDate' 的值转换为预期的参数类型 'NSDateComponents'
Swift: Cannot convert value of type 'NSDate' to expected argument type 'NSDateComponents'
有人知道我怎么解决这个问题吗?我收到此错误:
¨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
,它将 NSDateComponents
转换为 NSDate
,但提供了 competitionDate
,它已经是 NSDate
。编译器只是简单地通知你这个错误。
--
顺便说一句,您使用 .Hour
、.Minute
、.Second
和 NSDate
中的 .Nanosecond
填充 NSDateComponents
,但随后继续尝试保存对 components.year
、components.month
和 components.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)
}
}
有人知道我怎么解决这个问题吗?我收到此错误:
¨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
,它将 NSDateComponents
转换为 NSDate
,但提供了 competitionDate
,它已经是 NSDate
。编译器只是简单地通知你这个错误。
--
顺便说一句,您使用 .Hour
、.Minute
、.Second
和 NSDate
中的 .Nanosecond
填充 NSDateComponents
,但随后继续尝试保存对 components.year
、components.month
和 components.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)
}
}