Swift: Hashable struct with dictionary property

喜你入骨 提交于 2019-12-12 08:06:57

问题


I have a struct in Swift that looks like this:

internal struct MapKey {
    internal let id: String
    internal let values: [String:String]
}
extension MapKey: Equatable {}
func ==(lhs: MapKey, rhs: MapKey) -> Bool {
    return lhs.id == rhs.id && lhs.values == rhs.values
}

I now have the need to use MapKey as the key in a Swift dictionary, which requires MapKey to conform to the Hashable protocol.

What would a correct implementation of Hashable be for a struct like this one?

extension MapKey: Hashable {
    var hashValue: Int {
        return ??? // values does not have a hash function/property.
    }
}

I've been doing some research but failed to identify what the proper way to hash a dictionary is, as I need to be able to generate a hash value for values property itself. Any help is much appreciated.


回答1:


I think you need to review your data model if you have to use a whole struct as a dictionary key. Anyhow, here's one way to do it:

internal struct MapKey: Hashable {
    internal let id: String
    internal let values: [String:String]

    var hashValue: Int {
        get {
            var hashString = self.id + ";"
            for key in values.keys.sort() {
                hashString += key + ";" + values[key]!
            }

            return hashString.hashValue
        }
    }
}

func ==(lhs: MapKey, rhs: MapKey) -> Bool {
    return lhs.id == rhs.id && lhs.values == rhs.values
}

This assumes that you don't have semicolon (;) in id or in the keys and values of values. Hasable implies Equatable so you don't need to declare it conforming to Equatable again.




回答2:


Since both id and values are immutable both are ok to use as basis for equals and hashValue. However - if MapKey.id (which the name somewhat implies) uniquely identifies the MapKey (at least within the context of one dictionary) then it is both easier and more performant to just use the MakKey.id as basis for == operator as well as hashValue

    internal struct MapKey: Hashable {
        internal let id: String
        internal let values: [String:String]

        var hashValue: Int {
            get { return  self.id.hashValue}
        }
    }

    func ==(lhs: MapKey, rhs: MapKey) -> Bool {
        return lhs.id == rhs.id
    }



回答3:


foundation data types are Hashable in Swift 4.2, you only need to let your MapKey struct to conform Hashable protocol:

struct MapKey: Hashable {
    let id: String
    let values: [String: String]
}

in case you want to use a class, you need conform hash(:) func like this:

class MapKey: Hashable {
    static func == (lhs: MapKey, rhs: MapKey) -> Bool {
        return lhs.id == rhs.id && lhs.values == rhs.values
    }

    let id: String = ""
    let values: [String: String] = [:]

    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
        hasher.combine(values)
    }
}


来源:https://stackoverflow.com/questions/38000867/swift-hashable-struct-with-dictionary-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!