Swift 3 - Ambiguous use of < operator when comp

2019-06-16 06:24发布

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.

1条回答
孤傲高冷的网名
2楼-- · 2019-06-16 06:46

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.

查看更多
登录 后发表回答