6

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?

ByteHamster
  • 4,884
  • 9
  • 38
  • 53
drpecker26
  • 71
  • 1
  • 2
  • It isn't NSDate trickiness. It is Swift numerics trickiness... :( – matt Apr 22 '15 at 16:30
  • 1
    Unrelated to your problem, but don't assume that one day has 86400 seconds (think of daylight saving time transitions). Better use NSCalendar methods for all calendrical calculations. – Martin R Apr 22 '15 at 17:16

1 Answers1

6

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
Greg
  • 25,317
  • 6
  • 53
  • 62