Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 http://www.globalnerdy.com/2016/08/29/how-to-work-with-dates-and-times-in-swift-3-part-3-date-arithmetic/

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.

like image 734
Dallas Avatar asked Sep 17 '16 00:09

Dallas


1 Answers

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.

like image 60
Leo Dabus Avatar answered Oct 18 '22 05:10

Leo Dabus