Swift泛型和枚举作为数据源

问题描述

我有一个视图,该视图显示选项列表,并为用户提供选择其中一个选项的选项。此视图必须可重用,以与多组选项一起使用。我该怎么做。我尝试了以下方法,但是遇到了错误Protocol 'Options' can only be used as a generic constraint because it has Self or associated type requirements,我知道为什么会这样。还有更好的方法吗?

protocol Options: CaseIterable {
    var displayName: String { get }
}

enum MyOptions: Options {
    case hello
    case mello
    case fello
    
    static var allCases: [MyOptions] {
        [.hello,.mello]
    }
    
    var displayName: String {
        "hello"
    }
}

/// This is just for testing

func testContains(option: Options,options: [Options]) -> Bool {
    true
}

print(testContains(option: MyOptions.fello,options: MyOptions.allCases))

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)