3

假设我们struct在 Swift 中有一个相当大的:

struct SuperStruct {
    var field1: Int = 0
    var field2: String = ""
    // lots of lines...
    var field512: Float = 0.0
}

..然后我们需要实现Equatable协议:

extension SuperStruct: Equatable {
}

func ==(lhs: SuperStruct, rhs: SuperStruct) -> Bool {
    return
        lhs.field1 == rhs.field1 &&
        lhs.field2 == rhs.field2 &&
        // lots of lines...
        lhs.field512 == rhs.field512
}

...而且我们需要编写很多行愚蠢的代码。有没有办法“要求”编译器为我们“做”它?

4

4 回答 4

5

以下答案显示了一种可能的解决方案;可能不是推荐的(但可能对这个问题的未来读者感兴趣)。


如果您有大量属性都属于数量有限的不同类型,您可以使用Mirror您的结构实例并迭代结构的属性;对于每次尝试转换为您知道您的属性的不同类型。

在观看了以下 WWDC 2015 会议(感谢 Leo Dabus!)之后,我编辑了之前的答案(我认为它更简洁):

我也会将最初的答案留在这个答案的底部,因为它显示了一种替代的、较少面向协议的方法来利用这个Mirror解决方案。

Mirror& 面向协议的解决方案:

/* Let a heterogeneous protocol act as "pseudo-generic" type
   for the different (property) types in 'SuperStruct'         */
protocol MyGenericType {
    func isEqualTo(other: MyGenericType) -> Bool
}
extension MyGenericType where Self : Equatable {
    func isEqualTo(other: MyGenericType) -> Bool {
        if let o = other as? Self { return self == o }
        return false
    }
}

/* Extend types that appear in 'SuperStruct' to MyGenericType  */
extension Int : MyGenericType {}
extension String : MyGenericType {}
extension Float : MyGenericType {}
    // ...

/* Finally, 'SuperStruct' conformance to Equatable */
func ==(lhs: SuperStruct, rhs: SuperStruct) -> Bool {

    let mLhs = Mirror(reflecting: lhs).children.filter { $0.label != nil }
    let mRhs = Mirror(reflecting: rhs).children.filter { $0.label != nil }

    for i in 0..<mLhs.count {
        guard let valLhs = mLhs[i].value as? MyGenericType, valRhs = mRhs[i].value as? MyGenericType else {
            print("Invalid: Properties 'lhs.\(mLhs[i].label!)' and/or 'rhs.\(mRhs[i].label!)' are not of 'MyGenericType' types.")
            return false
        }
        if !valLhs.isEqualTo(valRhs) {
            return false
        }
    }
    return true
}

示例用法:

/* Example */
var a = SuperStruct()
var b = SuperStruct()
a == b // true
a.field1 = 2
a == b // false
b.field1 = 2
b.field2 = "Foo"
a.field2 = "Foo"
a == b // true

以前的Mirror解决方案:

/* 'SuperStruct' conformance to Equatable */
func ==(lhs: SuperStruct, rhs: SuperStruct) -> Bool {

    let mLhs = Mirror(reflecting: lhs).children.filter { $0.label != nil }
    let mRhs = Mirror(reflecting: rhs).children.filter { $0.label != nil }

    for i in 0..<mLhs.count {
        switch mLhs[i].value {
        case let valLhs as Int:
            guard let valRhs = mRhs[i].value as? Int where valRhs == valLhs else {
                return false
            }
        case let valLhs as String:
            guard let valRhs = mRhs[i].value as? String where valRhs == valLhs else {
                return false
            }
        case let valLhs as Float:
            guard let valRhs = mRhs[i].value as? Float where valRhs == valLhs else {
                return false
            }
            /* ... extend with one case for each type
            that appear in 'SuperStruct'  */
        case _ : return false
        }
    }
    return true
}

示例用法:

/* Example */
var a = SuperStruct()
var b = SuperStruct()
a == b // true
a.field1 = 2
a == b // false
b.field1 = 2
b.field2 = "Foo"
a.field2 = "Foo"
a == b // true
于 2016-01-26T21:47:42.673 回答
3

在 Swift 4.1 中,如果所有类型的成员都是 Equatable/Hashable,则 Equatable/Hashable 类型现在综合符合 Equatable/Hashable

SE-0185

综合 Equatable 和 Hashable 一致性

开发人员必须编写大量样板代码来支持复杂类型的等价性和散列性。该提议为编译器提供了一种自动综合符合 Equatable 和 Hashable 的方法,以减少这种样板,在已知可能生成正确实现的场景子集中。

https://github.com/apple/swift-evolution/blob/master/proposals/0185-synthesize-equatable-hashable.md

于 2018-05-21T20:45:31.123 回答
1

不,它没有。至少不是以任何不是过于复杂并且基于运行时内省的使用(滥用?)的方式。请参阅dfri 的回答,了解技术上可行的内容,但这比编写直接比较所有字段的实现要复杂得多==

至于您对 Swift 中“应该”提供什么的意见,如果您与 AppleSwift 开源社区分享它们,您更有可能看到一些效果。

于 2016-01-26T21:57:26.917 回答
1

您可以使结构可编码并比较 JSON 编码的数据。效率不高,但可能对某些应用程序有用(例如单元测试)。

struct SuperStruct: Encodable {
    var field1: Int = 0 
    // ....
    var field512: Float = 0.0
}

let s1 = SuperStruct()
let s2 = SuperStruct()

let encoder = JSONEncoder()
let data1 = try! encoder.encode(s1)
let data2 = try! encoder.encode(s2)
let result = (data1 == data2)

如果你喜欢这个,你可以把它整理成一个Encodable.

于 2018-02-18T22:14:58.143 回答