巢狀列舉

你可以將列舉巢狀在另一個內部,這樣你就可以構建層次結構的列舉,使其更有條理,更清晰。

enum Orchestra {
    enum Strings {
        case violin
        case viola
        case cello
        case doubleBasse
    }
    
    enum Keyboards {
        case piano
        case celesta
        case harp
    }
    
    enum Woodwinds {
        case flute
        case oboe
        case clarinet
        case bassoon
        case contrabassoon
    }
}

你可以像這樣使用它:

let instrment1 = Orchestra.Strings.viola
let instrment2 = Orchestra.Keyboards.piano