Swift 3 - Comparing Date objects

beeef picture beeef · Aug 18, 2016 · Viewed 138.2k times · Source

I'm updating my app to Swift 3.0 syntax (I know it's still in beta but I want to be prepared as soon as it released).

Until the previous Beta of Xcode (Beta 5) I was able to compare two Date objects using the operands <, > and ==. But in the latest beta (Beta 6) this isn't working any more. Here are some screenshots:

enter image description here enter image description here

As you can see in both screenshots, these are two Date objects. But I get the following error: enter image description here

What am I doing wrong? The functions are still declared in the Date class:

static func >(Date, Date)

Returns true if the left hand Date is later in time than the right hand Date.

Is this just a Beta bug or am I doing something wrong?

Answer

Ankit Thakur picture Ankit Thakur · Aug 18, 2016

I have tried this snippet (in Xcode 8 Beta 6), and it is working fine.

let date1 = Date()
let date2 = Date().addingTimeInterval(100)

if date1 == date2 { ... }
else if date1 > date2 { ... }
else if date1 < date2 { ... }