具有关联值的枚举不符合CaseIterable并引发错误 [英] Enum with associated value does not conform to CaseIterable and throws error

查看:117
本文介绍了具有关联值的枚举不符合CaseIterable并引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下枚举可以正常工作,没有任何错误。

The following enum works fine without any error.

enum EnumOptions: CaseIterable {
        case none
        case mild
        case moderate
        case severe
        case unmeasurable
}

当我尝试向其中一种情况添加关联值时,它引发以下错误类型'EnumOptions'不符合协议'CaseIterable'。是否要添加协议存根?

When I try to add an associated value to one of the cases, it throws the following error "Type 'EnumOptions' does not conform to protocol 'CaseIterable'. Do you want to add protocol stubs?"

enum OedemaOptions: CaseIterable {
        case none
        case mild
        case moderate
        case severe
        case unmeasurable(Int)
}

添加存根后,

enum OedemaOptions: CaseIterable {
        typealias AllCases = <#type#>

        case none
        case mild
        case moderate
        case severe
        case unmeasurable(Int)

在占位符中应填写什么以使Enum符合CaseIterable,因为只有1个案例具有关联值,而并非所有案例都如此?

What should be filled up in the placeholder to make the Enum conform to CaseIterable, since there is only 1 case with associated value and not all the cases?

推荐答案

自动综合不适用于具有关联值的枚举。您需要提供allCases属性的自定义实现。尝试,

Automatic synthesis does not work for enums with associated values. You need to provide a custom implementation of the allCases property. Try,

enum OedemaOptions: CaseIterable {
    static var allCases: [OedemaOptions] {
        return [.none, .mild, .moderate, .severe, .unmeasurable(-1)]
    }
    
    case none
    case mild
    case moderate
    case severe
    case unmeasurable(Int)
}

这篇关于具有关联值的枚举不符合CaseIterable并引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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