简体   繁体   English

从没有 switch/case 的枚举中获取关联值

[英]Get associated value from enumeration without switch/case

I've got an enumeration with a few different cases which are different types, eg我有一些不同类型的不同案例的枚举,例如

enum X {
    case AsInt(Int)
    case AsDouble(Double)
}

I can switch on these just fine to get the underlying value back out.我可以switch这些就好获得潜在价值退了出去。 However, the switch statement is highly annoying with trying to make me execute some code for the other cases that I simply don't care about.然而, switch语句非常烦人,它试图让我为我根本不关心的其他情况执行一些代码。 For example, right now I have something like例如,现在我有类似的东西

func AsInt(x: X) -> Int? {
    switch x {
    case AsInt(let num):
        return num;
    default:
        return nil;
    }
}

This works but it's pretty tedious always having to reference this method and having to write a new one for each case of each enumeration.这很有效,但总是必须引用此方法并且必须为每个枚举的每个案例编写一个新方法,这非常乏味。 What I'm looking for is how to simply attempt to cast a value of type X to one of the cases, like我正在寻找的是如何简单地尝试将X类型的值转换为其中一种情况,例如

var object: X = func();
let value = obj as? Int;
if value {
    // do shit
}

How can I simply check for a case without having to enumerate all of the cases about which I don't care and execute some non-statement for them?我怎样才能简单地检查一个案例而不必列举所有我不关心的案例并为他们执行一些非声明?

Bonus points for any solution that can declare value as part of the conditional instead of polluting the scope.任何可以将value声明为条件的一部分而不是污染范围的解决方案的奖励积分。

There are actually multiple ways to do it.实际上有多种方法可以做到。

Let's do it by extending your enum with a computed property:让我们通过使用计算属性扩展您的枚举来实现:

enum X {
    case asInt(Int)
    case asDouble(Double)

    var asInt: Int? {
        // ... see below
    }
}

Solutions with if case if case解决方案

By having let outside:通过let外面:

var asInt: Int? {
    if case let .asInt(value) = self {
        return value
    }
    return nil
}

By having let inside:通过let里面:

var asInt: Int? {
    if case .asInt(let value) = self {
        return value
    }
    return nil
}

Solutions with guard caseguard case解决方案

By having let outside:通过let外面:

var asInt: Int? {
    guard case let .asInt(value) = self else {
        return nil
    }
    return value
}

By having let inside:通过let里面:

var asInt: Int? {
    guard case .asInt(let value) = self else {
        return nil
    }
    return value
}

The last one is my personal favorite syntax of the four solutions.最后一个是我个人最喜欢的四种解决方案的语法。

As of Swift 2 (Xcode 7) this is possible with if/case and pattern matching:从 Swift 2 (Xcode 7) 开始,这可以通过if/case和模式匹配实现:

let x : X = ...
if case let .AsInt(num) = x {
    print(num)
}

The scope of num is restricted to the if-statement. num的范围仅限于 if 语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift在没有开关的情况下获取枚举中的关联值 - Swift get associated value in enums without switch Swift 有没有办法在不使用 switch 语句的情况下获取关联值? - Is there a way in Swift to get an associated value without using a switch statement? swift enum在单个switch-case中获取具有相同参数的多个case的关联值 - swift enum get the associated value of multiple case with same parameters in single switch-case 从没有开关的枚举中获取关联类型 object - Get associated type object from enum without switch 如何访问本身位于尾随闭包中的 switch case 的关联值 - How to access associated value of a switch case, which itself is in a trailing closure 如何在Swift 4中通过原始值获取枚举用例的名称? - How to get the name of an enumeration case by its raw value in Swift 4? Swift:从枚举中的“索引”获取双值 - Swift: Get double value from 'index' in enumeration 是否有可能在枚举关联值条件和另一个枚举情况之间写复合开关案例? - Is it possible to write compound switch case between enum associated value conditional and another enum case? 如果枚举的案例具有关联类型,是否可以在没有关联数据的情况下获取案例名称? - If an enum's case has an associated type, is it possible to get the case name without the associated data? 使用关联值时获取枚举名称 - Get enumeration name when using associated values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM