Advanced Java enums in Swift

后端 未结 5 951
萌比男神i
萌比男神i 2021-01-03 15:40

I have a number of Java classes I need to convert to Swift code. One of the classes has an advanced enum:

public enum Student {

  STUDENT_ONE(\"Steve\", \"J         


        
5条回答
  •  一向
    一向 (楼主)
    2021-01-03 16:32

    I was trying to do the same thing with converting Java code to Swift, and ended up doing something like this :

    public enum Student {
    
        case STUDENT_ONE
        case STUDENT_TWO
    
        var firstName: String {
            get {
                switch self {
                case .STUDENT_ONE:
                    return "Steve"
                case .STUDENT_TWO:
                    return "Tim"
                }
            }
        }
    
        var lastName: String {
            get {
                switch self {
                case .STUDENT_ONE:
                    return "Jobs"
                case .STUDENT_TWO:
                    return "Cook"
                }
            }
        }
    }
    

    Now, this is really long and messy and I'm not really sure whether this is the right way to do it, but I couldn't find anything else that worked. I would love to know if there is some other better way to do it.

提交回复
热议问题