如何在枚举列表中查找枚举?

时间:2017-03-24 15:47:04

标签: swift

我想确定枚举列表中是否存在枚举。 直觉我会这样做:

if myEnum == (.one || .two) { return true }
else { return false }

当然它不起作用。

我知道我能做到:

if myEnum == .one || myEnum == .two { return true }
else { return false }

if [EnumType.one, EnumType.two].contains(myEnum) { return true }
else { return false }

但我今天只想表现得很好看。)

我正在考虑使用过滤器,但它似乎有点过分。

你有什么想法吗?

非常感谢。

亨利

5 个答案:

答案 0 :(得分:1)

你可以做到

//: Playground - noun: a place where people can play

import Cocoa

enum MyEnum {
    case one
    case two
    case three
    case four
}

let list: [MyEnum] = [.one, .two, .three, .four]

if list.contains(.one) {
    // Contains
} else {
    // Doesn't Contain
}

如果您有相关数据,则必须使您的枚举为Equatable才能使其正常工作。例如:

//: Playground - noun: a place where people can play

import Cocoa

enum MyEnum: Equatable {
    case one
    case two
    case three
    case four
    case other(String)

    static func ==(lhs: MyEnum, rhs: MyEnum) -> Bool {
        switch (lhs, rhs) {
        case (.one, .one),
             (.two, .two),
             (.three, .three),
             (.four, .four):
            return true

        case (.other(let lhsString), .other(let rhsString)) where lhsString == rhsString:
            return true

        default:
            return false
        }
    }
}

let list: [MyEnum] = [.one, .two, .three, .four, .other("test")]

if list.contains(.one) {

} else {

}

答案 1 :(得分:1)

我会对每一个进行切换,然后如果找不到这些类型,则会有一个默认值。

switch myEnum {
    case .one:
        print("One is here baby")
    case .two:
        print("Two is here baby")
    default:
        print("Can't find the case??????")
    }

答案 2 :(得分:1)

那是OptionSet的用途。它在技术上是struct,但在使用中,看起来非常接近enum

struct MyOptions : OptionSet {
    var rawValue: Int
    init(rawValue: Int) { self.rawValue = rawValue }

    static let one = MyOptions(rawValue: 1)
    static let two = MyOptions(rawValue: 2)
    static let three = MyOptions(rawValue: 4)
}


let option: MyOptions = [.one, .two]

if option.contains([.one, .two]) {
    print("Has one and two")
}

if !option.intersection([.one, .three]).isEmpty {
    print("Has one or three")
}

答案 3 :(得分:0)

我也会使用一个开关,并将使用通用逻辑处理的枚举情况分组如下:

enum MyEnum {
    case one
    case two
    case three
    case four
}

switch myEnum {
    case .one, .two:
        //deal with enum cases .one and .two
    default:
        //deal with all other cases
    }
}

答案 4 :(得分:0)

如果你试图将任意字符串与你的枚举中的各种情况相匹配,那么你可以做这样的事情(使用Swift 3)。

enum CompassPoint:String {
    case north
    case south
    case east
    case west

    static func has(key: String) -> Bool {
        for f in iterateEnum(CompassPoint.self) {
            if(f.rawValue == key) {
                return true;
            }
        }
        return false;
    }

    private static func iterateEnum<T: Hashable>(_: T.Type) -> AnyIterator<T> {
        var i = 0
        return AnyIterator {
            let next = withUnsafeBytes(of: &i) { $0.load(as: T.self)   }
            if next.hashValue != i { return nil }
            i += 1
            return next
        }
    }
}

CompassPoint.has(key: "south") // true
CompassPoint.has(key: "something") // false

向@rintaro致敬,了解迭代枚举的功能(见下文)。 https://stackoverflow.com/a/28341290/5270038

相关问题