ios - swift 2.0 : Cannot convert NSDateComponents to NSCalendarUnit

标签 ios swift nscalendar

这是接收问题代码的代码行:

let CompetitionDayDifference = userCalendar.components (dayCalendarUnit, fromDate: currentDate!, toDate: competitionDay, options: nil)

问题代码:

Cannot convert value of type NSDateComponents to expected argument type NSCalendarUnit

附加代码:

let userCalendar = NSCalendar.currentCalendar()


let competitionDate = NSDateComponents()
competitionDate.year = 2015
competitionDate.month = 6
competitionDate.day = 21
competitionDate.hour = 08
competitionDate.minute = 00
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 = userCalendar.components (dayCalendarUnit, fromDate: currentDate!, toDate: competitionDay, options: [])

最佳答案

根据错误,您的 dayCalendarUnit 是 NSDateComponents 类型的变量。您应该改为传递 NSCalendarUnit。从 Swift 2.0 开始,您也不能为选项参数传递 nil,因此您需要传递一个空选项集:

let CompetitionDayDifference = userCalendar.components(NSCalendarUnit.Day, fromDate: currentDate!, toDate: competitionDay, options: [])

关于ios - swift 2.0 : Cannot convert NSDateComponents to NSCalendarUnit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33766829/

相关文章:

iphone - 如何使用 NSCalendar 或 NSDate 将 30 天生成为数组?

ios - 如何创建没有工具栏的全屏相机界面?

ios - 在 swift 中在矩形上绘制渐变

IOS - 为什么标签没有更新?

ios - CABasicAnimation CALayer 路径动画方向

ios - 如何快速显示来自API的图像?

ios - swift 本月的第一天和最后一天

ios - Xcode 无法识别 GoogleAPIClientForREST?

ios - Roboto 字体在 iOS 13 中的 iPhone XR 上无法使用

xcode - Swift - 检查日期是否在下周/下月。 ( isDateInNextWeek() isDateInNextMonth() )