Default value for Enum in Swift

前端 未结 11 2032
南旧
南旧 2020-12-31 07:30

I have an enum :

public enum PersonType:String {

 case Cool                       = \"cool\"
 case Nice                       = \"rude\"
 case          


        
11条回答
  •  清酒与你
    2020-12-31 07:47

    I wonder if dictionary is not a better fit than enum here:

    let dict = [
        "Cool": "cool",
        "Nice": "rude",
        "SoLazy": "so-lazy"
    ]
    
    let personType = "unknown"
    let personDescription = dict[personType] ?? "Unknown"
    

    Less typing, faster processing, more natural handling of the default case, easier to expand.

提交回复
热议问题