English 中文(简体)
多个审案
原标题:Checking for multiple enum cases
  • 时间:2024-01-04 23:07:06
  •  标签:
  • swift
  • enums

我有一大批案件,有几项职能,可以检查是否有结构。

我现在就这样说:

public enum Type { 
    case A1
    case A2
    case A3
    case B1
    case B2
    case C1
    case C2
    case C3
    case C4
    case C5
    case C6
    //
}

struct myStruct {
    public var type: Type = .A1

    func isA() -> Bool {
       return (type == .A1 || type == .A2 || type == .A3)
    }
}

这项工作,但我很想知道我是否能够简化这项工作。 我尝试:

func isA() -> Bool {
   return (type == .A1 || .A2 || .A3)
}

但有一点错误:

不能将类型价值转换为预期的Bool类型

Bool型没有成员A1

Bool型没有成员A2

Bool型没有成员A3

是否有办法加以简化?

问题回答

我可能只是检查在一套或“阿雷”内的成员:

public enum Type { 
    case A1
    case A2
    case A3
    // ...

    var isA: Bool { [.A1, .A2, .A3].contains(self) }
}

For completeness, you could also use pattern matching within a switch statement:

var isA: Bool {
    switch self {
        case .A1, .A2, .A3: return true
        default: return false
    }
}

如果你使你的遗体具有某种内涵,而且不会凌驾于缺损的原材料(每例从零开始,每例从一例中增加一例),那么你就能够利用不平等来看待某个特定遗体是否属于其他一系列。

public enum Type: Int, CaseIterable { 
    case A1
    case A2
    case A3
    case B1
    case B2
    case C1
    case C2
    case C3
    case C4
    case C5
    case C6
    //
}


if let anEnum = Type.allCases.randomElement() {
    let lowest = Type.B1
    let highest = Type.C2
    let isInRange = anEnum.rawValue >= lowest.rawValue && anEnum.rawValue <= highest.rawValue
    let isInRangeString = isInRange ? "is" : "is not"
    print("enum (anEnum) (isInRangeString) in range (lowest) to (highest)")
}

(In the code above I also make Type conform to CaseIterable so I can use the computed property allCases to create a random item.)

如果你想测试一下,使用固定成员或开关声明(如“Alexander”所描述)来判断遗体是否属于任意的 en价值清单。





相关问题
Finding the Highest Value in an Enumeration

I m writing a method which determines the highest value in a .NET enumeration so I can create a BitArray with one bit for each enum value: pressedKeys = new BitArray(highestValueInEnum<Keys>());...

Conversion of Enum to Enumerable

To convert Enum to Enumerable ,I use public enum Flags { Trivial=1, Minor, Major, Critical } IEnumerable<int> n = Enumerable.Range((int)Flags.Trivial, (...

Subclass check, is operator or enum check

A couple of friends was discussing the use of inheritance and how to check if a subclass is of a specific type and we decided to post it here on Stack. The debate was about if you should implement a ...

Enum scoping issues

I try to keep things as local as possible, so I put enums at class scope, even if they are shared between two classes (I put it in the class that "goes better" with it.) This has worked out great, but ...

How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

C++ enum value initialization

I have an enum declared in my code as: enum REMOTE_CONN { REMOTE_CONN_DEFAULT = 0, REMOTE_CONN_EX_MAN = 10000, REMOTE_CONN_SD_ANNOUNCE, REMOTE_CONN_SD_IO, REMOTE_CONN_AL, ...

WCF: Enforce equal DataContracts on both sides

I m wondering if it is possible to have WCF make sure that the DataContracts on both sides of a connection are exactly the same (and throw an exception when trying to connect if they are not). For ...

热门标签