3

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", "")
}

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

Comments

3

should be like this:

enum ProType {
    case fund
    case stock

    var gCode: String {
        switch self {
        case .fund:
            return "FN"
        case .stock:
            return "VA"
        }
    }

    var cCode: String {
        switch self {
        case .fund:
            return "PP"
        case .stock:
            return ""
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.