How should I implement Default Associated Values with Swift Enums?

前端 未结 3 1441
没有蜡笔的小新
没有蜡笔的小新 2021-02-20 09:27

Swift question is there a way of having an enum type with one case that can have an associated value.

I have an API that gives me available filters, it\'s unlikely but p

3条回答
  •  一整个雨季
    2021-02-20 09:59

    I know this is a bit old, but would this work for what you want?

    typealias FilterIdentifier = String
    
    enum DefaultAPIFilters: FilterIdentifier {
        case Everyone = "everyone"
        case Team = "team"
    }
    
    enum APIFilters {
        case Default(DefaultAPIFilters)
        case Custom(FilterIdentifier)
    }
    
    let everyoneFilter = APIFilters.Default(.Everyone)
    let teamFilter = APIFilters.Default(.Team)
    let clownFilter = APIFilters.Custom("clowns_only")
    

提交回复
热议问题