swiftnsdatenstimeinterval

Swift ios date as milliseconds Double or UInt64?


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,


Solution

  • Swift does not allow comparing different types.

    seconds is a Double floating point value in seconds with sub-second accuracy.
    sampleDate is a UInt64 but the units are not given. sampleDate needs be converted to a Double floating point value with units of seconds.

    var sampleDate: Double = Double(sample.getStartDate())
    

    Then they can be compared:

    if(sampleDate > cuttDate){}