Compare two instances of an object in Swift

27,233

Solution 1

Indicate that your class conforms to the Equatable protocol, and then implement the == operator.

Something like this:

class PLClient: Equatable 
{
    var name = String()
    var id = String()
    var email = String()
    var mobile = String()
    var companyId = String()
    var companyName = String()
    //The rest of your class code goes here

    public static func ==(lhs: PLClient, rhs: PLClient) -> Bool{
        return 
            lhs.name == rhs.name &&
            lhs.id == rhs.id &&
            lhs.email == rhs.email &&
            lhs.mobile == rhs.mobile &&
            lhs.companyId == rhs.companyId &&
            lhs.companyName == rhs.companyName
    }
}

Solution 2

Working off of Duncan C's answer, I have come up with an alternative which is slightly clearer that it is being used in a custom way:

// Client Object
//
class PLClient {
    var name = String()
    var id = String()
    var email = String()
    var mobile = String()
    var companyId = String()
    var companyName = String()

    convenience init (copyFrom: PLClient) {
        self.init()
        self.name = copyFrom.name
        self.email = copyFrom.email
        self.mobile = copyFrom.mobile
        self.companyId = copyFrom.companyId
        self.companyName = copyFrom.companyName   
    }

    func equals (compareTo:PLClient) -> Bool {
        return
            self.name == compareTo.name &&
            self.email == compareTo.email &&
            self.mobile == compareTo.mobile
    }

}

var clientOne = PLClient()
var clientTwo = PLClient(copyFrom: clientOne)

if clientOne.equals(clientTwo) {
    println("No changes made")
} else {
    println("Changes made. Updating server.")
}
Share:
27,233
Michael Voccola
Author by

Michael Voccola

Updated on August 08, 2022

Comments

  • Michael Voccola
    Michael Voccola over 1 year

    Given the following class, how can all the values in two instances be compared to each other?

    // Client Object
    //
    class PLClient {
        var name = String()
        var id = String()
        var email = String()
        var mobile = String()
        var companyId = String()
        var companyName = String()
    
        convenience init (copyFrom: PLClient) {
            self.init()
            self.name =  copyFrom.name
            self.email = copyFrom.email
            self.mobile = copyFrom.mobile
            self.companyId = copyFrom.companyId
            self.companyName = copyFrom.companyName
    
        }
    
    }
    
    var clientOne = PLClient()
    
    var clientTwo = PLClient(copyFrom: clientOne)
    
    if clientOne == clientTwo {   // Binary operator "==" cannot be applied to two PLClient operands
        println("No changes made")
    } else {
        println("Changes made. Updating server.")
    }
    

    The use-case for this is in an application which presents data from a server. Once the data is converted into an object, a copy of the object is made. The user is able to edit various fields etc.. which changes the values in one of the objects.

    The main object, which may have been updated, needs to be compared to the copy of that object. If the objects are equal (the values of all properties are the same) then nothing happens. If any of the values are not equal then the application submits the changes to the server.

    As is shown in the code sample, the == operator is not accepted because a value is not specified. Using === will not achieve the desired result because these will always be two separate instances.