I am trying to figure out how to duplicate my Java Enum into Swift and i don't know if this is the right way.
My Enum in Java which i am trying to write in Swift:
public enum EnumDB {
    DATABASE_NAME("DataBase"),
    DATABASE_VERSION(1);
    private String name;
    private int value;
    private EnumDB(String name) {
        this.name = name;
    }
    private EnumDB(int value) {
        this.value = value;
    }
    public String getName() {
        return name;
    }
    public int getValue() {
        return value;
    }
}
My Swift Code:
enum EnumDB {
    case Name,Version
    func getName() -> String{
        switch self{
        case .Name: return "DataBase"
        }
    }
    func getNumber() -> Int{
        switch self{
        case .Version: return 1
        default: return 0
        }
    }
}
My questions are:
thanks
You can definitely have an enum with associated values of different types, which I think can give you what you're looking for. This is how I might implement your example:
enum EnumDB {
    case Name(String)
    case Version(Int)
}
let namedDB = EnumDB.Name("databaseName")
switch namedDB {
case .Name(let name):
    println("Database name is \(name)")
case .Version(let versionNumber):
    println("Database version is \(versionNumber)")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With