0
public func ==(lhs: Date, rhs: Date) -> Bool {
  return lhs === rhs || lhs.compare(rhs) == .orderedSame
}

After upgrading to Swift 3 I now get this error... "Binary operator ===cannot be applied to two Date operands"

Any suggestions on how to correct this?

BoilingLime
  • 2,207
  • 3
  • 22
  • 37
Mikhail Kulin
  • 51
  • 2
  • 11
  • 3
    Swift 3 Date is a Struct that already conforms to Comparable protocol. http://stackoverflow.com/questions/39541879/swift-3-ambiguous-use-of-operator-when-comparing-two-dates/39541975#39541975 – Leo Dabus Dec 01 '16 at 16:44

4 Answers4

5

In Swift 3 Date is a structure (value type), there is no identities and thus cannot be ==='ed.

In fact, the Date type already provides a built-in == so you don't need to implement it yourself.

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • *there is no identities* what does that mean? – mfaani Dec 01 '16 at 17:01
  • 2
    @Honey Identity = Reference = Pointer value. Structures having the same values are indistinguishable from each other, i.e. you cannot assign identities to each struct value. See http://stackoverflow.com/a/25334176/224671. – kennytm Dec 01 '16 at 17:09
1

Dummy code snippet for future reference. Main part is use timeIntervalSinceNow that will convert to Double and then compare

let inputFormatter = DateFormatter()
        inputFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
        inputFormatter.dateFormat = "MMM dd, yyyy hh:mm:ss a"

        dataArray = dataArray.sorted(by: { (crm1 : CRM, crm2 : CRM) -> Bool in
             inputFormatter.date(from: crm1.date1!)!.timeIntervalSinceNow <= inputFormatter.date(from: crm2.date1!)!.timeIntervalSinceNow
        })
jeet.chanchawat
  • 5,842
  • 5
  • 38
  • 59
0

Leo Dabus pointed out an article SOLUTION POST

I used >= to correct the problem.

Community
  • 1
  • 1
Mikhail Kulin
  • 51
  • 2
  • 11
  • You can use any comparison operator. Just remove all custom operators you had previously defined for NSDate – Leo Dabus Dec 01 '16 at 18:03
0

I have solved it by force unwraping the date objects

let secondF = DateFormatter()
secondF.dateFormat = "yyyy-MM-dd"

let startdate = secondF.date(from: "2020-03-01")
let endDate = secondF.date(from: "2020-02-01")

if startdate! == endDate!{
   print("succedded")
}
Abuzar Manzoor
  • 379
  • 4
  • 13