Shift72
Shift72

Reputation: 448

Swift 2.0: Cannot convert NSDateComponents to NSCalendarUnit

Here is the line of code that receives the issue code:

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

Issue code:

Cannot convert value of type NSDateComponents to expected argument type NSCalendarUnit

Additional code:

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: [])

Upvotes: 0

Views: 907

Answers (1)

Charles A.
Charles A.

Reputation: 11123

Based on the error, your dayCalendarUnit is a variable of type NSDateComponents. You should be passing an NSCalendarUnit instead. You also cannot pass nil for the options parameter as of Swift 2.0, so you would need to pass an empty option set:

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

Upvotes: 2

Related Questions