简体   繁体   中英

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.

您可能已经扩展了NSDate以符合Swift 2中的类似协议。只需删除它,因为Date现在符合Swift3中的Comparable协议。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM