Given the following class, how can you compare all values ββin duplicate with 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.") }
An example of a use for this is in an application that presents data from a server. After converting the data to an object, a copy of the object is created. The user can edit various fields, etc., which changes the values ββin one of the objects.
The main object that may have been updated must be compared with a copy of this object. If the objects are equal (the values ββof all properties are the same), nothing happens. If any of the values ββis not equal, then the application sends the changes to the server.
As shown in the sample code, the == operator is not accepted because the value is not specified. Using === will not produce the desired result, because it will always be two separate instances.
source share