2014/06/09

Swift 列舉(Enumerations)



//
//  main.swift
//  test
//
//  Created by CYFang on 6/6/14.
//  Copyright (c) 2014 CYFang. All rights reserved.
//

//return Array to Integer
class Animal{

    enum AnimalEnum : Int{
        case Cat = 1
        case Dog = 2
        case Tiger = 3
        case Monkey = 4
        case Zebra = 5
        case Elephants, Whale
    
        func Name()->String{
            switch self{
                case .Cat:
                    return "Cat"
                case .Dog:
                    return "Dog"
                case .Tiger:
                    return "Tiger"
                case .Monkey:
                    return "Monkey"
                case .Zebra:
                    return "Zebra"
                case .Elephants:
                    return "Elephants"
                case .Whale:
                    return "Whale"
                default:
                    return "People"
            }
        }
    }
}

var mType = Animal.AnimalEnum.Zebra
println("\(mType.toRaw()):\(mType.Name())")
println("\(Animal.AnimalEnum.fromRaw(1)?.toRaw()):\(Animal.AnimalEnum.fromRaw(1)?.Name())")



執行結果: