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.
You have probably extended
NSDate
to conform to comparable protocol in Swift 2. Just remove it becauseDate
now conforms to Comparable protocol in Swift3.