Swift ios date as milliseconds Double or UInt64?

snaggs picture snaggs · Aug 12, 2014 · Viewed 27.3k times · Source

I'm not iOS developer but started learning Swift.

I try to convert some logic from Android project to iOS.

I have the following method:

func addGroupItemSample(sample : WmGroupItemSample){ // some custom class

    var seconds: NSTimeInterval = NSDate().timeIntervalSince1970
    var  cuttDate:Double =  seconds*1000;

    var sampleDate: UInt64 = sample.getStartDate(); // <-- problematic place

if(sampleDate > cuttDate){
   // ....
  }
}

From the method above you can see that sample.getStartDate() returns type UInt64.

I thought it's like long in Java: System.currentTimeMillis()

But current time in milliseconds defined as Double.

Is it a proper way to mix Double and UInt64 or do I need to represent all milliseconds as Double only?

Thanks,

Answer

Ben picture Ben · Jan 11, 2015

in iOS it is better to use double, but if you want to easy port your code and keep it consistent you can try this:

func currentTimeMillis() -> Int64{
    let nowDouble = NSDate().timeIntervalSince1970
    return Int64(nowDouble*1000)
}