2019年6月10日 星期一

Enumeration 列舉

enum SomeEnumeration{
    //成員
}
//定義enum
enum CompassPoint{
    case north
    case south
    case east
    case west
}
//定義enum時,指定資料類型Int,會給予成員rawValue,從0開始
enum Planet:Int{
    case mercury, venus, earth, mars, jupiter, ssturn, uranus, neptuneㄤ;
}
print(Planet.earth.rawValue)
//實作
var directionToHead:CompassPoint = CompassPoint.west
directionToHead = .east
//directionToHead = .south
//利用switch檢查
switch directionToHead {
case .north:
    print("north");
case .south:
    print("south");
case .east:
    print("east");
case .west:
    print("west");
}
//let button = UIButton(type:UIButton.ButtonType.infoLight);
let button = UIButton(type: .infoLight);
//建立可以被for in的列舉,一定要有CaseIterable,CaseIterable:可重複使用
enum Beverage:CaseIterable{
    case coffee, tea, juice
}
//allCases:Enumeration內的所有case
print(Beverage.allCases.count)
for beverage in Beverage.allCases{
    print(beverage)
}

沒有留言:

張貼留言