Home > AI > Language > Swift >

enum

Points:

  1. Basic format
  2. enum with type declaraion
  3. enum case with predefined value
  4. enum with function
  5. enum case with variable

Example 1: enum has type declaration and function

enum ImageEnum: String {
    case img1 = "1"
    case img2 = "2"
    case img3 = "3"

    func next() -> ImageEnum {
        switch self {
            case .img1: return .img2
            case .img2: return .img3
            case .img3: return .img1
        }
    }
}


print(ImageEnum.img1.next())

Example 2: the most basic format

enum MoveDirection {
    case forward
    case back
    case left
    case right
}
let a = MoveDirection.forward
print(a) // forward
print(type(of: a)) // MoveDirection

Example 3: basic format with type declaration, then you can access the rawValue attribute

enum MoveDirection: String {
    case forward
    case back
    case left
    case right
}
let a = MoveDirection.forward
print(a) // forward
print(type(of: a)) // MoveDirection
print(a.rawValue)
print(type(of: a.rawValue))

Example 4: with predefined value and you can also check the difference with a and a.rawValue.

enum MoveDirection : String {
    case forward = "you moved forward"
    case back = "you moved backwards"
    case left = "you moved to the left"
    case right = "you moved to the right"
}

let a = MoveDirection.forward
print(a) // forward
print(type(of: a)) // MoveDirection
print(a.rawValue)
print(type(of: a.rawValue))

Example 5: predefined value (implicit)

enum GolfScores : Int {
    case HoleInOne = 1, Birdie, Boogie, Par4, Par5, Par6
    
    func getScore() -> Int {
        return self.rawValue
    }

}

var shot = GolfScores.Birdie
print(shot) // this will print out 2
print(shot.getScore())

Example 6: enum case with variable and with an outside assistant function.

enum BankDeposit {
    case inValue(Int, Int, Int) //hundreds,tens,ones
    case inWords(String) //words
}


func makeDeposit(_ person:BankDeposit){
    switch person{
    case .inValue(let hundred, let tens, let ones):
        print("deposited: \((hundred*100)+(tens*10)+(ones*1))")
    case .inWords(let words):
        print("deposited: \(words)")
    }
}

var person1 = BankDeposit.inValue(1,2,5)
makeDeposit(person1) //prints deposited: 125

var person2 = BankDeposit.inWords("One Hundred Twenty Five")
makeDeposit(person2) //prints deposited: One Hundred Twenty Five

Example 7: enum case with variable without outside assistant function

enum BankDeposit {
    case inValue(Int, Int, Int) //hundreds,tens,ones
    case inWords(String) //words
}



var person1 = BankDeposit.inValue(1,2,5)
print(person1)

var person2 = BankDeposit.inWords("One Hundred Twenty Five")
print(person2)

Example 8: nested enum

enum PTEQuestionType {
    enum PTEReadType: String {
        case RA
        
        func getFullName() -> String {
            switch self {
                case .RA:
                    return "Read aloud"
                }
        }
        
        func getShortname() -> String {
            return self.rawValue
        }
    }
}


let a = PTEQuestionType.PTEReadType.RA
print(a.getShortname())
Relevant tags:

Leave a Reply