Swift 3 - 含糊不清地使用 <比较两个日期时的运算符 [英] Swift 3 - Ambiguous use of < operator when comparing two dates

查看:25
本文介绍了Swift 3 - 含糊不清地使用 <比较两个日期时的运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 swift 中比较两个日期时,我可以使用 > 进行比较,但不能使用 <.startTime、endTime 和 Date() 都是 Date 类型(以前是 NSDate)

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?

我实际上是在尝试查找苹果文档时找到了这个示例,并且他们实际上使用了此代码http://www.globalnerdy.com/2016/08/29/how-to-work-with-dates-and-times-in-swift-3-part-3-date-arithmetic/

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 {...}

但它返回相同的顺序.

显然我找到了解决方法,但我很好奇为什么会发生这种情况.

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

推荐答案

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

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.

这篇关于Swift 3 - 含糊不清地使用 &lt;比较两个日期时的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆