Shift72
Shift72

Reputation: 448

Swift 2.0: Value of type 'NSDateComponentsFormatter' has no member 'Day'

I receive the error on Day, Hour and Minute. Here is the code:

let dayCalendarUnit = calendar.components([NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Minute], fromDate: date)


//here we change the seconds to hours,minutes and days
let CompetitionDayDifference = NSDateComponentsFormatter()
CompetitionDayDifference.unitsStyle = .Full
CompetitionDayDifference.allowedUnits = [.Day, .Hour, .Minute]
let string = CompetitionDayDifference.stringFromDate(currentDate!, toDate: competitionDay)

//finally, here we set the variable to our remaining time
var daysLeft = CompetitionDayDifference.Day
var hoursLeft = CompetitionDayDifference.Hour
var minutesLeft = CompetitionDayDifference.Minute

Upvotes: 0

Views: 1291

Answers (1)

vadian
vadian

Reputation: 285069

The error occurs because you're trying to get date component properties directly from a date formatter which is impossible.

Actually you don't need a date formatter. Just get the components from the difference

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

Upvotes: 1

Related Questions