swift - 是否可以让枚举类型将它们存储在 Swift 4 的集合中?
问题描述
我有多个符合特定协议的 Enum 类型,我想将它们的类型存储在一个数组中。但我似乎找不到办法做到这一点。
这是简化的代码。
protocol TestEnumProtocol {
static var name: String { get }
static var count: Int { get }
}
enum Test1 : TestEnumProtocol {
static var name: String { return "Test1" }
static var count: Int { return 2 }
case A
case B
}
enum Test2 : TestEnumProtocol {
static var name: String { return "Test2" }
static var count: Int { return 3 }
case AA
case BB
case CC
}
var enums: [TestEnumProtocol.self] = [Test1.self, Test2.self] // Compile Error
基本上,我需要处理多个多变量测试,并且我想让我的逻辑通用。
我知道我可以通过使用类来实现这一点,但我想知道是否有使用枚举的方法,因为 Enum 在这种情况下似乎更直观。(至少对我来说)
解决方案
推荐阅读
- python - Python:加载excel表头而不加载剩余数据
- php - 无法在单元测试中运行 laravel artisan 命令
- java - 如何将数据从 Activity 扩展类共享到 View 扩展类?
- spring-boot - Spring Boot:将自动装配的构造函数与配置文件中的类一起使用
- python - 使用 Python 或任何语言从 Sql 文件中提取源表和目标表
- vb.net - 我喜欢通过vb.net datagridview删除oracle表多条记录,下面的编码出现ora 00936缺少表达式错误
- kubernetes - 使用检索集群详细信息时出现禁止错误
使用 REST Kubernetes API - html - 您可以使用 grid-auto-flow: dense 而不知道您将拥有多少网格项目
- android - 无法下载 builder.jar Android Studio 4.0
- python - 如何在 tkinter 按钮中调用 self?