比较 Swift 中对象的两个实例

Compare two instances of an object in Swift

给定以下class,如何将两个实例中的所有值相互比较?

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

此用例是在呈现来自服务器的数据的应用程序中。一旦数据被转换为对象,就会生成该对象的副本。用户能够编辑各种字段等。这会更改其中一个对象中的值。

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

如代码示例所示,== 运算符未被接受,因为未指定值。使用 === 不会获得预期的结果,因为它们始终是两个独立的实例。

表明你的class符合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
    }
}

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

您可以使用 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: [=10=]] == rhs[keyPath: [=10=]] }
    }
}

尝试 swift 中的“is”关键字, 例如

if self.navigationController.topViewController is TestViewController {
//Logic here
}