gpt4 book ai didi

ios - swift 2.0 : Cannot convert NSDateComponents to NSCalendarUnit

转载 作者:行者123 更新时间:2023-11-28 13:01:14 24 4
gpt4 key购买 nike

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

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/

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