Advanced Java enums in Swift

后端 未结 5 962
萌比男神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:30

    After some thought, I agree with godmoney that aksh1t's solution is better that my solution using Strings.

    Anyway, here is a more concise variant of aksh1t's solution, using only one computed property returning a tuple: (tested in Swift 2.0)

    enum Student {
        case STUDENT_ONE, STUDENT_TWO
    
        typealias Details = (firstName: String, lastName: String)
        var details : Details {
            switch(self) {
            case STUDENT_ONE : return ("Steve", "Jobs")
            case STUDENT_TWO : return ("Tim", "Cook")
            }
        }
    }
    
    // Usage:
    func test(sd: Student.Details) {
        print(sd.firstName)
        print(sd.lastName)
    }
    test(Student.STUDENT_ONE.details)
    

提交回复
热议问题