Default value for Enum in Swift

前端 未结 11 1982
南旧
南旧 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:50

    For you case:

    Default Value of Enum: I just add an default computed property, Or include an customize init.

    public enum PersonType:String {
    
        case Cool                       = "cool"
        case Nice                       = "rude"
        case SoLazy                     = "so-lazy"
    
        /// add a `default` computer property
        public static var `default`: PersonType {
            return .SoLazy
        }
    
        /// add an customize init function 
        public init(person: String? = nil) {
            if let person = person {
                switch person {
                case "cool": self = .Cool
                case "rude": self = .Nice
                case "so-lazy": self = .SoLazy
                default: self = .SoLazy
                }
            } else {
                self = .SoLazy
            }
        }
    
        public var description: String {
            switch self {
            case .Cool:
                return "Cool person"
            case .Nice:
                return "Nice person"
            case .SoLazy:
                return "its so lazy person"
            }
        }
    
        public var typeImage: String {
            switch self {
            case .Cool:
                return "cool.png"
            case .Nice:
                return "img_nice.png"
            case .SoLazy:
                return "lazy.png"
            }
        }
    
    }
    

    To use:

    if let personType = PersonType(rawValue:personTypeKey ?? "") {
        self.personType = personType
    } else {
        self.personType = PersonType.default
    }
    

    Or

    if let personType = PersonType(rawValue:personTypeKey ?? "") {
        self.personType = personType
    } else {
        self.personType = PersonType()
    }
    

    Default Value of Enum With Associated Value:

    public enum Gender {
        case man
        case woman
    }
    
    public enum PersonType {
    
        case cool(Gender)
        case nice(Gender)
        case soLazy(Gender)
    
        public static var `default`: PersonType {
            return PersonType.make.soLazy()
        }
    
        public enum Builder {
            public static func cool() -> PersonType {
                return PersonType.cool(.woman)
            }
            public static func nice() -> PersonType {
                return PersonType.nice(.woman)
            }
            public static func soLazy() -> PersonType {
                return PersonType.soLazy(.woman)
            }
        }
    
        public static var make: PersonType.Builder.Type {
            return PersonType.Builder.self
        }
    
    
        public var description: String {
            switch self {
            case .cool(let gender):
                switch gender {
                case .man: return "Cool boy"
                case .woman: return "Cool girl"
                }
            case .nice(let gender):
                switch gender {
                case .man: return "Nice boy"
                case .woman: return "Nice girl"
                }
            case .soLazy(let gender):
                switch gender {
                case .man: return "its so lazy boy"
                case .woman: return "its so lazy girl"
                }
            }
        }
    
        public var typeImage: String {
            switch self {
            case .cool(_):
                return "cool.png"
            case .nice(_):
                return "img_nice.png"
            case .soLazy(_):
                return "lazy.png"
            }
        }
    
    }
    

    To use:

    let onePersonType = PersonType.default
    let anotherPersonType = PersonType.make.soLazy()
    

    The second case solution I was found on Ilya Puchka' blog. And also it's mentioned in swift's proposal.

提交回复
热议问题