標籤:ast each ant number ret arc 整型 Plan back
001-枚舉
//枚舉 swift 中不會被賦於預設的整型值enum someEnumeration { case north case sourth case east case west}//多個成員值要寫在一行,用逗號隔開enum Planet { case mercury,earth,mars}var directionHead = someEnumeration.west directionHead = .east //使用Switch匹配單個的枚舉值 switch directionHead { case .north: print("north") case .sourth: print("sourth") case .west: print("west") case .east: print("east") default: print("None Of This Direction") }
//枚舉關聯值
enum BarCode { case upc(Int,Int,Int,Int) //根據數字識別商品 case qrCode(String) //根據代碼識別商品}//建立一個條碼 var productBar = BarCode.upc(8, 8, 3, 4) productBar = .qrCode("ABCDE")//upc被qrCode代替
//枚舉原始值
//枚舉的原始值 隱士賦值enum ASCIIControlChar : Character { case tab = "\t" case lineFeed = "\n" case carriageReturn = "\r"}//字串的預設值是枚舉值本身enum Planet:Int{ case mercury = 1,venus,earth,mars}//擷取枚舉變數的原始值 let earthOrder = Planet.earth.rawValue print(earthOrder)//輸出結果為 3
//枚舉遞迴
//枚舉遞迴indirect enum Arithmetic { case number(Int) case addition(Arithmetic,Arithmetic) case multiplication (Arithmetic,Arithmetic)} //建立一個枚舉遞迴運算式 let five = Arithmetic.number(5) let four = Arithmetic.number(4) let sum = Arithmetic.addition(five, four) let product = Arithmetic.multiplication(sum, five) // (5+4) * 5 print(product)
//類
//類的聲明:class Student{ var name:String = "" var age:Int = 18}//訪問屬性 classItem.name = "小子" print(classItem.name)//小子
//結構體:
//結構體struct teacher { var name:String = "" var age:Int = 20}let myTeacher = teacher() print(myTeacher)//teacher(name: "", age: 20)
Swift-類、結構體、枚舉