21

I have two objects for the same struct class having different values. I need to compare these two objects whether they are equal or not. Please guide me through a proper solution.

struct CustomerInfo {
   var billAddressCity: String?

   init(a:String?){
      billAddressCity = a

  }
}

 /* init object */
 var obj1 =  CustomerInfo?
 var obj2 =  CustomerInfo?

 obj1 = CustomerInfo(a: "ONE")
 obj2 = CustomerInfo(a: "TWO")

 /*  I need to compare these two objects */

 if obj1 == obj2 {
     Print(equal values)
  }

This is not the answer Iam looking for, as it says i need to compare each and every values of fields manually, Compare two instances of an object in Swift

Jan
  • 1,744
  • 3
  • 23
  • 38
  • 1
    Your struct needs to conform to the Equatable protocol to use '==' for comparision – Ashutosh Dave Sep 06 '17 at 12:10
  • _"This is not the answer I am looking for"_ Well, that's the answer regardless... – jscs Sep 06 '17 at 12:38
  • I absolutely get what's being asked here, and would like to do the same thing: when a struct has 30 attributes, I'd like to know if the attributes of obj1 are all equal to the same attributes of obj2, ***without*** having to write (and ***maintain!***) a 30-item-long comparison, like `obj1.abc == obj2.abc && obj1.bcd == obj2.bcd && ...`. Seems like there ought to be an elegant way to do this! – ConfusionTowers Mar 11 '21 at 19:47

2 Answers2

40

In the Object Oriented World, generally, all Objects are unique to each other.

So, how I could compare two objects?

Your struct/class need to implement the Equatable Protocol which let you define how an object could be equal to another object of the same class/struct

Let's begin with this House struct:

struct House {
    var street: String
}

var houseA = House.init(street: "street A, n. 10")
var houseB = House.init(street: "street A, n. 10")

houseA == houseB // of course you can't

With the Equatable protocol you can implement the static func ==. Here you can define how two object of the same class could be equal when you use the operator ==.

In my example, two objects of the struct House could be equal when they have the same street:

struct House: Equatable {
    var street: String

    static func == (lhs: House, rhs: House) -> Bool {
        return lhs.street == rhs.street
    }
}  

var houseA = House.init(street: "street A, n. 10")
var houseB = House.init(street: "street A, n. 10")

houseA == houseB // now print true
Giuseppe Sapienza
  • 4,101
  • 22
  • 23
  • 4
    Unfortunately this question has been closed. But for structs, another option (which might be useful in certain situations is:) func areStructsEqual(s1: T1, s2: T2) -> Bool { var s1 = s1 var s2 = s2 let s1Data = Data(bytes: &s1, count: MemoryLayout.stride) let s2Data = Data(bytes: &s2, count: MemoryLayout.stride) return s1Data == s2Data } Basically the idea is comparing the raw data of the structs byte by byte. I tested this with ints, and then a few different types of custom structs. Unfortunately I can't format the code here xD – Rufus Mall Jan 02 '20 at 22:08
5

By conforming to Equatable:

struct CustomerInfo: Equatable {
    var billAddressCity: String?

    init(a:String?){
        billAddressCity = a
    }

    static func ==(lhs: CustomerInfo, rhs: CustomerInfo) -> Bool {
        return (lhs.billAddressCity == rhs.billAddressCity)
    }
}
sCha
  • 1,454
  • 1
  • 12
  • 22
  • how do i call this method? – Jan Sep 06 '17 at 12:13
  • do i need to compare each and every field of structure? I'm looking for alternative way. – Jan Sep 06 '17 at 12:15
  • If you define `CustomerInfo` in this way, you can simply do `if obj1 == obj2`. This comparison will call `static func ==` and return results by comparing `billAddressCity`. – sCha Sep 06 '17 at 12:15
  • @Jan then how do you want to compare them? give us examples. – sCha Sep 06 '17 at 12:18