Swift Enum of custom types conform to hashable protocol

时间:2015-10-29 16:01:28

标签: swift swift2

I have an enum like this:

enum Animals {
    case Cow     (MyCowClass)
    case Bird    (MyBirdClass)
    case Pig     (MyPigClass)
    case Chicken (MyChickenClass)
}

Each of the types conform to the hashable protocol. This enum then becomes a property of a different class:

class Farm {
    let name = "Bob's Farm"
    var animal = Animals

    required init(animal: Animals) {
        self.animal = animal
    }

I would like to get the hash value from the instance of the case and use that for the enum so I can make the Farm class hashable using the name & animal.

2 个答案:

答案 0 :(得分:8)

您可以使Animals哈希,例如:

enum Animals : Hashable {
    case Cow     (MyCowClass)
    case Bird    (MyBirdClass)
    case Pig     (MyPigClass)
    case Chicken (MyChickenClass)

    var hashValue: Int  {
        switch self {
        case .Cow(let v): return v.hashValue
        case .Bird(let v): return v.hashValue
        case .Pig(let v): return v.hashValue
        case .Chicken(let v): return v.hashValue
        }
    }
}
func ==(lhs: Animals, rhs: Animals) -> Bool {
    return ...
}

同样对于班级Farm,例如:

class Farm : Hashable  {

    var hashValue: Int {
        return [name.hashValue, animal.hashValue].hashValue
    }
}
func ==(lhs: Farm, rhs: Farm) -> Bool {
    return ...
}

最后,Ints容器实现了一个属性hashValue

extension CollectionType where Generator.Element: Int {
    var hashValue: Int {
        return ...
    }
}

对于适当的算法,您可以搜索网络 - 例如:http://www.eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx

答案 1 :(得分:3)

接受答案的替代方法,假设您不希望基于关联对象的枚举如下:

private enum AnimalType: Int {
    case Cow = 0
    case Bird = 1
    case Pig = 2
    case Chicken = 3
}

func == (lhs: Animals, rhs: Animals) -> Bool {
    return lhs.hashValue == rhs.hashValue
}

enum Animals: Hashable {
    case Cow     (MyCowClass)
    case Bird    (MyBirdClass)
    case Pig     (MyPigClass)
    case Chicken (MyChickenClass)

    var hashValue: Int {
        get {
            switch self {
            case .Cow(_):
                return AnimalType.Cow.hashValue
            }
        }
    }
}
相关问题