Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift code in playground vs. an actual class

Odd thing happening here. The following code:

String(Int(date.timeIntervalSince1970*1000))

works in a playground but doesn't in a class. It crashes with EXC_BAD_INSTRUCTION.

Any ideas why? Alternatively, how do I get a string of the above NSTimeInterval?

Thanks in advance!

Edit: I need the result as Int, so 1402324472549 and not 1402324472549.64, for example.

like image 228
Stelian Iancu Avatar asked Feb 12 '23 18:02

Stelian Iancu


2 Answers

Inside a class definition:

var date = NSDate()
let myDateString = String(Int64(date.timeIntervalSince1970()*1000))
println("Seconds = \(myDateString)")
like image 133
hotpaw2 Avatar answered Feb 15 '23 07:02

hotpaw2


Not sure of the root cause, but it is something to do with the type conversion. This works:

var date = NSDate()
var asString = "\(Int(date.timeIntervalSince1970*1000))"
println(asString)
like image 42
ColinE Avatar answered Feb 15 '23 08:02

ColinE