Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin enum classes in Swift

I would like to use this Kotiln code in Swift, but I don't know how to get the best and clean solution:

enum class ProType(val gCode: String, val cCode: String) {
    FUND("FN", "PP"),
    STOCK("VA", "")
}
like image 522
Ricardo Avatar asked Feb 20 '26 18:02

Ricardo


1 Answers

Technically @esemusa answer is right. But if you have more than ~5 values in enum, you end up with difficult to maintain giant switch statements for every property.

So for cases like that I prefer to do this:

struct ProTypeItem {
    var gCode: String
    var cCode: String
}

struct ProType {
    static let fund = ProTypeItem(gCode: "FN", cCode: "PP")
    static let stock = ProTypeItem(gCode: "VA", cCode: "")
}

And you use it simply as ProType.stock, ProType.fund.gCode etc

You can also make ProTypeItem Comparable, Equatable etc.

like image 62
ytrewq Avatar answered Feb 26 '26 09:02

ytrewq