二元运算符“~=”不能应用于两个 UmRet 操作数

Binary operator '~=' cannot be applied to two UmRet operands

我正在努力将 IDTech 刷卡器集成到我的应用程序中,我已经取得了很大进展,添加了库,注册了通知,取消注册了它们,现在我正在开发一个连接 reader。当我尝试根据 return 值切换大小写时,我似乎无法弄清楚我在这里做错了什么。有人可以帮我吗?

func displayUmRet(operation: String, returnValue: UmRet) {

        var string = ""

    do {
        switch returnValue {

        case UMRET_SUCCESS: string = ""
        case UMRET_NO_READER: string="No reader attached"
        case UMRET_SDK_BUSY: string="Communication with reader in progress"
        case UMRET_MONO_AUDIO: string="Mono audio enabled"
            case UMRET_ALREADY_CONNECTED: string="Already connected"
            case UMRET_LOW_VOLUME: string="Low volume"
            case UMRET_NOT_CONNECTED: string="Not connected"
            case UMRET_NOT_APPLICABLE: string="Not applicable to reader type"
            case UMRET_INVALID_ARG: string="Invalid argument"
            case UMRET_UF_INVALID_STR: string="Invalid firmware update string"
            case UMRET_UF_NO_FILE: string="Firmware file not found"
            case UMRET_UF_INVALID_FILE: string="Invalid firmware file"
            default: string="<unknown code>"
        }
    } while (0)

   // var retStatus = UMRET_SUCCESS==ret

    //self.textResponse.text = "\(operation), \(retStatus), \(string)"

    self.hexResponse.text = "";


}

你需要在你的案例前加上一个.:

enum UmRet {
  case UMRET_SUCCESS, UMRET_FAILURE
}

var string = " "

let returnValue = UmRet.UMRET_SUCCESS

switch returnValue {
case .UMRET_SUCCESS: string = "y"
case .UMRET_FAILURE: string = "n"
}

此外,0 与 Swift 中的 false 不同,因此:

do {
...
} while (0)

也不应该工作。

而且行尾不需要分号,所以:

self.hexResponse.text = "";

可以是这样的:

self.hexResponse.text = ""

最后,如果您的 switch 语句包含枚举中每个案例的每个案例,则您不需要默认案例。 (这就是为什么我的例子中没有)

顺便说一下,~= 只是模式匹配函数的运算符,这就是 Swift 在 switch 语句中所做的。它的工作方式有点像 == 函数,例如,Int ~= IntInt == Int 相同。但它更通用一些:例如 Range ~= Int,例如 0...3 ~= 2 returns 无论 Int 是否在范围内。 (在这种情况下如此)对于枚举,它会将案例与案例进行匹配。在我的示例中,它将匹配 UMRET_SUCCESS,并且 string 将设置为 y