Dallas Dallas - 5 months ago 235
Swift Question

Swift 3 - Ambiguous use of < operator when comparing two dates

When comparing two Dates in swift, I can compare using >, but not <.
startTime, endTime and Date() are all of type Date (previously NSDate)

//Broken Code
if Date() >= startTime && Date() < endTime
Gives ambiguous use of < operator error

//Working Code
if Date() >= startTime && endTime > Date()

Is there a specific reason this isn't working?

I actually found this example when trying to find the apple documentation, and they actually use this code

I started wondering if maybe it was the using of the && operator, or possibly just being an issue of the order, but even doing the code by itself as

if startTime < endTime {...}

But it returns the same order.

Obviously I have found the workaround, But I am very curious why this is happening.


You have probably extended NSDate to conform to comparable protocol in Swift 2. Just remove it because Date now conforms to Comparable protocol in Swift3.