Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble Using Swift NSDate "timeIntervalSinceNow" [duplicate]

I've searched around and am flummoxed by this riddle.

In Swift, Xcode 6.2, these lines work:

let day_seconds = 86400
let one_day_from_now = NSDate(timeIntervalSinceNow:86400)

But the following returns an error:

let day_seconds = 86400
let one_day_from_now = NSDate(timeIntervalSinceNow:day_seconds)

Console output:

"Playground execution failed: /var/folders/4n/88gryr0j2pn318sw_g_mgkgh0000gn/T/lldb/10688/playground625.swift:24:30: error: extra argument 'timeIntervalSinceNow' in call let one_day_from_now = NSDate(timeIntervalSinceNow:day_seconds)"

What's going on here? Why the NSDate trickiness?

like image 935
drpecker26 Avatar asked Apr 22 '15 16:04

drpecker26


1 Answers

It's because timeIntervalSinceNow expect NSTimeInterval which is Double.

If you do:

let day_seconds = 86400

day_second is Int type which is not what the method expect. However when you type the number itself:

let one_day_from_now = NSDate(timeIntervalSinceNow:86400)

compiler implicit that you are passing Double, because it's what the method expect, which is ok.

The solution cancould be using NSTimeInterval(day_seconds) or Double(day_seconds) which is the same or when you declare constant make sure it's double, for example:

let day_seconds = 86400.0

or

let day_seconds: Double = 86400

or

let day_seconds: NSTimeInterval = 86400
like image 199
Greg Avatar answered Sep 28 '22 21:09

Greg