如何使用Xcode 10中可用的API使枚举符合Hashable?

时间:2019-01-04 11:26:20

标签: swift xcode10 swift4.2 hashable

在我的Swift 4.2.1代码中,我有以下枚举:

enum MyEnum {

    case caseOne(Int)
    case caseTwo(String)
    case caseThree
}

它符合Equatable

extension MyEnum: Equatable {

    static func == (lhs: MyEnum, rhs: MyEnum) -> Bool {

        switch (lhs, rhs) {
        case (.caseOne, .caseOne), (.caseTwo, .caseTwo), (.caseThree, .caseThree):
            return true
        default:
            return false
        }
    }
}

我需要使其符合Hashable,这就是为什么我添加扩展名的原因:

extension MyEnum: Hashable {

    var hashValue: Int {

        switch self {
        case .caseOne:
            return 1
        case .caseTwo:
            return 2
        case .caseThree:
            return 3
        }
    }
}

现在,我想迁移到Xcode 10中可用的新API。我删除了hashValue的实现,并添加了hash(into:)的实现:

extension MyEnum: Hashable {

    func hash(into hasher: inout Hasher) {

        switch self {
        case .caseOne:
            hasher.combine(1)
        case .caseTwo:
            hasher.combine(2)
        case .caseThree:
            hasher.combine(3)
        }
    }
}

能否请您告诉我是否正确切换到新API?我使用此测试,如果一切正常,它将打印true两次:

var testDictionary = [MyEnum: Int]()
testDictionary[.caseOne(100)] = 100
testDictionary[.caseOne(1000)] = 1000
testDictionary[.caseTwo("100")] = 100
testDictionary[.caseTwo("1000")] = 1000
let countCaseOne = testDictionary.reduce(0) {
    if case .caseOne = $1.key {
        return $0 + 1
    }
    return $0
} == 1
print(countCaseOne) // true
let countCaseTwo = testDictionary.reduce(0) {
    if case .caseTwo = $1.key {
        return $0 + 1
    }
    return $0
} == 1
print(countCaseTwo) // true

2 个答案:

答案 0 :(得分:4)

不需要手动实现Hashable一致性,编译器可以自动综合您特定的四个enum(其中所有具有关联值的情况都有一个Hashable关联值)。您只需要声明符合性即可。

enum MyEnum: Hashable {
    case caseOne(Int)
    case caseTwo(String)
    case caseThree
}

// You don't even need to write `: Equatable`, since automatic Hashable conformance takes care of Equatable too, I just left it there for clarity
extension MyEnum: Equatable {
    static func == (lhs: MyEnum, rhs: MyEnum) -> Bool {
        switch (lhs, rhs) {
        case (.caseOne, .caseOne), (.caseTwo, .caseTwo), (.caseThree, .caseThree):
            return true
        default:
            return false
        }
    }
}

答案 1 :(得分:1)

您可以使用自动生成的Hashable一致性,如另一个答案中所建议的(条件是您的类型不包含非Hashable类型的任何日期)。

但这就是您在一般情况下可以做的(自动生成的代码也可能看起来像这样):

extension MyEnum: Hashable {

    func hash(into hasher: inout Hasher) {

        switch self {
        case .caseOne(let value):
            hasher.combine(value) // combine with associated value, if it's not `Hashable` map it to some `Hashable` type and then combine result
        case .caseTwo(let value):
            hasher.combine(value) // combine with associated value, if it's not `Hashable` map it to some `Hashable` type and then combine result
        case .caseThree:
            // you can `combine` with some `Hashable` constant, but here it's ok just to skip
            break
        }
    }
}