How should I implement Default Associated Values with Swift Enums?

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

    According to this answer: Default value for Enum in Swift

    I recommend using such an approach

    public enum Result {
        case passed(hint: String)
        case failed(message: String)
    
        static let passed: Self = .passed(hint: "")
    }
    
    
    let res: Result = Result.passed
    

提交回复
热议问题