关联值和原始值可以在 Swift 枚举中共存吗? [英] Can associated values and raw values coexist in Swift enumeration?

查看:27
本文介绍了关联值和原始值可以在 Swift 枚举中共存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift 书上有例子分别演示了关联值和原始值,有没有办法将两个特性一起定义枚举?

我尝试将它们组合起来,但出现错误:

enum 条码 :String {case UPCA(Int, Int, Int) = "Order 1"//原始类型的枚举不能有带参数的 casecase QRCode(String) = "Order 2"//原始类型的枚举不能有带参数的 case}

解决方案

是的,这是可能的.枚举可能包含关联值和原始值.(Swift 5 和 4)

简写符号

您的代码的问题在于您使用了速记符号来表示RawRepresentable定义关联类型.

让我们看看如何分别定义这些:

1) RawRepresentable(速记符号):

enum 条码:字符串 {案例UPCA =订单1"case QRCode = "订单 2"}

2) 关联类型:

enum 条码 {案例 UPCA(Int, Int, Int)案例二维码(字符串)}

这些都很棒,但是如果您的代码片段显示两者都需要怎么办.

解决方案

定义具有关联值的枚举,然后在扩展中实现对 RawRepresentable 单独 的一致性(即不使用速记符号).

示例:

enum 条码 {案例 UPCA(Int, Int, Int)案例二维码(字符串)}扩展条码:RawRepresentable {公共类型别名 RawValue = String///失败初始化器公共初始化?(rawValue:RawValue){切换原始值{案例订单 1":self = .UPCA(1,1,1)case "Order 2": self = .QRCode("foo")默认:返回零}}///支持原始值公共变量 rawValue:RawValue {切换自我{案例.UPCA:返回订单1"case .QRCode:返回订单2"}}}

次要细节

在此解决方案中,关联值的默认值,例如从 rawValue 参数构造枚举时必须提供 .UPCA(1,1,1).您可以想象并使用关联类型作为支持原始值的一部分 - 这更强大,但增加了一些复杂性.

参考资料

有关该主题的更多信息,请参阅Ole Begemann 的优秀文章.>

There are examples on Swift book demonstrating associated values and raw values separately, is there a way to define enums with the two features together?

I have tried to combine them, but got errors:

enum Barcode :String {
    case UPCA(Int, Int, Int) = "Order 1" // Enum with raw type cannot have cases with arguments
    case QRCode(String) = "Order 2" // Enum with raw type cannot have cases with arguments
}

解决方案

Yes this is possible. An enum may contain both associated values and raw values. (Swift 5 & 4)

Shorthand notation

The issue with your code is that you are using the shorthand notation for RawRepresentable and defining associated types.

Let's look at how to define these separately:

1) RawRepresentable (shorthand notation):

enum Barcode: String {
    case UPCA   = "order 1"
    case QRCode = "order 2"
}

2) Associated types:

enum Barcode {
    case UPCA(Int, Int, Int)
    case QRCode(String)
}

Each of these is great, but what if you need both as your code snippet shows.

Solution

Define the enum with associated values and then implement conformance to RawRepresentable separately in an extension (i.e. not using shorthand notation).

Example:

enum Barcode {
    case UPCA(Int, Int, Int)
    case QRCode(String)
}

extension Barcode: RawRepresentable {

    public typealias RawValue = String

    /// Failable Initalizer
    public init?(rawValue: RawValue) {
        switch rawValue {
        case "Order 1":  self = .UPCA(1,1,1) 
        case "Order 2":  self = .QRCode("foo")
        default:
            return nil
        } 
    }

    /// Backing raw value
    public var rawValue: RawValue {
        switch self {
        case .UPCA:     return "Order 1"
        case .QRCode:   return "Order 2"
        }
    }

}

Minor Detail

In this solution, defaults for the associated values, e.g. .UPCA(1,1,1) must be supplied when constructing the enum from the rawValue argument. You can get fancy and use the associated types as part of the backing raw value — which is more powerful, but adds some complexity.

References

For more info on the topic see Ole Begemann's excellent write up.

这篇关于关联值和原始值可以在 Swift 枚举中共存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆