ios - 关联的枚举类型实现 CaseItratable

标签 ios swift enums

我有两个枚举和一个关联的枚举。我需要一次从枚举 A 和枚举 B 中获取所有可能的情况。

enum A {
    case a
    case b
    case c
}

enum B {
    case d
    case e
    case f
}

enum C {
    case first(A)
    case second(B)
}

extension C: CaseItratable {
 //How to implement?
}

在枚举 C 中需要一个 allCases 方法,它返回枚举 A 中的所有案例和枚举 B 中的所有案例

最佳答案

1. 符合 enum Aenum BCaseIterable协议(protocol)

enum A: CaseIterable {
    case a, b, c
}

enum B: CaseIterable {
    case d, e, f
}

2. 获取 enum A 的所有案例和 enum B使用 allCases .
enum C {
    case first(A)
    case second(B)

    var casesOfA: [A] {
        return A.allCases //here...
    }

    var casesOfB: [B] {
        return B.allCases //here...
    }
}

关于ios - 关联的枚举类型实现 CaseItratable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58499440/

相关文章:

iphone - MPMoviePlayerController 播放在视频结束前终止

ios - SCNNode 查看当前位置并始终相对向上

ios - CollectionView水平滚动约束问题

java - 加载图像时枚举抛出错误

c - 为什么对 C 枚举定义中的值使用 Bitwise-Shift 运算符?

ios - 为什么 Swift 2.2 不支持固定大小的数组,也许 swift 3.0 支持?

javascript - 检测 iOS 自动完成建议按键?

ios - 设备连接到Xcode时,应用工作正常,但在特定位置安装ipa时失败

ios - inputAccessoryView 在 alertController (actionSheet) 出现时动画化

rust - 匹配语句中明显未使用的变量