Swift Enumeration

Enumeration are used to combine common data type element into an single unit. This unit element are usable within our program. The value of enumeration variable may be in form of string, character, float and integer type. There are defined by using enum keyword.

enum EnumNames {
    // enumeration definition
}

Normally properties of enumeration are use in form of specified cases.

enum Result {
    //Define cases
    case pass
    case fail
    case panding 
}
let testCase: Result = .panding
print("You are result is : \(testCase)")

Output

panding

Associated values of Enums case

enum Action {
    case runing
    case stop
    case pending(wait: Int)
}
let d =  Action.pending(wait: 50)
print(d) //pending(50)

Output

pending(50)

Nested Enums

enum Movie{
    
    enum South{
        case action
        case doubleAction
        case comedy
        case superHero
    }
    enum Hollywood{
        case action
        case horror
        case adventure
        case terrorism
    }
    enum Bollywood{
        case dramatically
        case comedy
        case crime
        case drama
        case romantic
        case historical
    }
}
//Get nested enum values
let south  = Movie.South.action
let hollywood = Movie.Hollywood.adventure
let bollywood = Movie.Bollywood.drama

print("\(type(of:south)) :  \(south) ")
print("\(type(of:hollywood)) :  \(hollywood) ")
print("\(type(of:bollywood)) :  \(bollywood) ")

Output

South :  action 
Hollywood :  adventure 
Bollywood :  drama 


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved