Problem using Swift NSDate "timeIntervalSinceNow"

I searched around and am confused 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: additional argument" timeIntervalSinceNow "in the call let one_day_from_now = NSDate (timeInterval day_dayInterval day_timeInterval day

What's going on here? Why is the complexity of NSDate?

+6
source share
1 answer

This is because timeIntervalSinceNow expect NSTimeInterval , which is Double .

If you do:

 let day_seconds = 86400 

day_second - type Int, which is not expected. However, when entering the number itself:

 let one_day_from_now = NSDate(timeIntervalSinceNow:86400) 

implicit that you pass Double, because that is what the method expects, which is normal.

The solution can be used NSTimeInterval(day_seconds) or Double(day_seconds) , which is the same, or when you declare a constant, make sure it is doubled, for example:

 let day_seconds = 86400.0 

or

 let day_seconds: Double = 86400 

or

 let day_seconds: NSTimeInterval = 86400 
+6
source

Source: https://habr.com/ru/post/985697/


All Articles