How should I implement Default Associated Values with Swift Enums?

前端 未结 3 1415
没有蜡笔的小新
没有蜡笔的小新 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:54

    Extending Nathan Perry's response:

    You can add a

    var underlyingString: String {
      return getUnderlyingString(self) 
    }
    

    to the enum. Then define

    func getUnderlyingString(apiFilter: APIFilters) -> String { 
        switch apiFilter {
        case .Default(let defaultAPIFilter):
            return defaultAPIFilter.rawValue
        case .Custom(let custom):
            return custom
        }
    }
    

提交回复
热议问题