I want to have init from rawValue for my enum in Swift to return default value if rawValue init will return nil. Right now I have something like this:
public init(fromRawValue: String){
self = Language(rawValue: fromRawValue) ?? .English
}
I don't like this because it is completely new initializer. I tried to make something like this:
public init(rawValue: String){
self = Language(rawValue: fromRawValue) ?? .English
}
But I have got runtime exception with bad access. Can I somehow make it work or I just have to use this new and I cannot override original one to not be optional?
I would like to know if it possible to override original init from rawValue not workaround with completely new one that is using failable one.
The default initializer is failable
. It means that if the received parameter does not match a valid enum case it does return nil
.
Now you want to do 2 incompatibles things:
This is not possible, I the 3 possible solutions as follows:
You define a new not failable initializer with a default value, a different parameter name and inside it you call the default failable initializer.
enum Language: String {
case english = "English", italian = "Italian", french = "French"
init(fromRawValue: String) {
self = Language(rawValue: fromRawValue) ?? .english
}
}
You redefine the default initializer, you make it not failable and you write the full logic inside it.
enum Language: String {
case english = "English", italian = "Italian", french = "French"
init(rawValue: String) {
switch rawValue {
case "Italian": self = .italian
case "French": self = .french
default: self = .english
}
}
}
enum Language: String {
case english = "English", italian = "Italian", french = "French"
static func build(rawValue: String) -> Language {
return Language(rawValue: rawValue) ?? .english
}
}
Now you can build a Language
value writing:
let italian = Language.build(rawValue: "Italian") // Italian
let defaultValue = Language.build(rawValue: "Wrong input") // English