0

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: [])
Shift72
  • 426
  • 4
  • 21
  • 1
    You may need to include the whole line of code and the declaration/definition statements for the variables you use if you want someone to help you. – Charles A. Nov 17 '15 at 21:00
  • Probably a duplicate of [Swift 2.0 calendar components error](http://stackoverflow.com/questions/30769387/swift-2-0-calendar-components-error). – Martin R Nov 17 '15 at 21:02
  • @MartinR He also appears to be passing the wrong type for the first parameter (in addition to the options problem) – Charles A. Nov 17 '15 at 21:05
  • By the way, if your intent to show this elapsed time to the end-user, you can bypass some of this stuff and go directly to `NSDateComponentsFormatter`, For example `let formatter = NSDateComponentsFormatter(); formatter.unitsStyle = .Full; formatter.allowedUnits = [.Day, .Hour, .Minute]; let string = formatter.stringFromDate(firstDate, toDate: secondDate);` – Rob Nov 18 '15 at 17:34

1 Answers1

2

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: [])
Charles A.
  • 10,685
  • 1
  • 42
  • 39