比较Swift中对象的两个实例

时间:2015-05-07 15:18:54

标签: class swift object comparison

鉴于以下类,如何将两个实例中的所有值相互比较?

// 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.")
}

用例是在一个从服务器提供数据的应用程序中。将数据转换为对象后,将生成对象的副本。用户可以编辑各种字段等。这会改变其中一个对象的值。

可能已更新的主要对象需要与该对象的副本进行比较。如果对象相等(所有属性的值相同),则没有任何反应。如果任何值不相等,则应用程序将更改提交给服务器。

如代码示例所示,由于未指定值,因此不接受==运算符。使用===将无法获得所需的结果,因为它们将始终是两个独立的实例。

3 个答案:

答案 0 :(得分:29)

表明您的类符合Equatable协议,然后实现==运算符。

这样的事情:

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
    }
}

答案 1 :(得分:6)

根据Duncan C的回答,我提出了一个替代方案,它更加清晰,以自定义方式使用:

// 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.")
}

答案 2 :(得分:0)

您可以使用keypath

遍历字段

我还没有测试过,但是总体思路就在那里。提供有效字段的列表并遍历它们,而不是编写每个相等的字段。因此,它与@ duncan-c建议的相同,但具有循环功能。

类似的东西:

class PLClient:Equatable {
    var name = String()
    var id = String()
    var email = String()
    var mobile = String()
    var companyId = String()
    var companyName = String()

    public static func ==(lhs: PLClient, rhs: PLClient) -> Bool{
        let keys:[KeyPath<PLClient, String>] = [\.name, \.id, \.email, \.mobile, \.companyId, \.companyName]
        return keys.allSatisfy { lhs[keyPath: $0] == rhs[keyPath: $0] }
    }
}