小编Dou*_*les的帖子

如何将Swift枚举用作词典键?(符合Equatable)

我已经定义了一个枚举来代表一个"站"的选择; station由唯一的正整数定义,因此我创建了以下枚举,以允许负值表示特殊选择:

enum StationSelector : Printable {
    case Nearest
    case LastShown
    case List
    case Specific(Int)

    func toInt() -> Int {
        switch self {
        case .Nearest:
            return -1
        case .LastShown:
            return -2
        case .List:
            return -3
        case .Specific(let stationNum):
            return stationNum
        }
    }

    static func fromInt(value:Int) -> StationSelector? {
        if value > 0 {
            return StationSelector.Specific(value)
        }
        switch value {
        case -1:
            return StationSelector.Nearest
        case -2:
            return StationSelector.LastShown
        case -3:
            return StationSelector.List
        default:
            return nil
        }
    }

    var description: String { …
Run Code Online (Sandbox Code Playgroud)

enums dictionary hashable swift

35
推荐指数
2
解决办法
2万
查看次数

标签 统计

dictionary ×1

enums ×1

hashable ×1

swift ×1