directionToHead = .south switch directionToHead { case .north: print("Lots of planets have a north") case .south: print("Watch out for penguins") case .east: print("Where the sun rises") case .west: print("Where the skies are blue") } // Prints "Watch out for penguins"
enumBeverage: CaseIterable{ case coffee, tea, juice } let numberOfChoices =Beverage.allCases.count print("\(numberOfChoices) beverages available") // Prints "3 beverages available"
for beverage inBeverage.allCases { print(beverage) } // coffee // tea // juice
枚举类型可以有关联值。
enumBarcode{ case upc(Int, Int, Int, Int) case qrCode(String) }
var productBarcode =Barcode.upc(8, 85909, 51226, 3)
productBarcode = .qrCode("ABCDEFGHIJKLMNOP")
用 Switch 处理这些关联值。
switch productBarcode { case .upc(let numberSystem, let manufacturer, let product, let check): print("UPC: \(numberSystem), \(manufacturer), \(product), \(check).") case .qrCode(let productCode): print("QR code: \(productCode).") } // Prints "QR code: ABCDEFGHIJKLMNOP."
enumCompassPoint: String{ case north, south, east, west }
let earthsOrder =Planet.earth.rawValue // earthsOrder is 3
let sunsetDirection =CompassPoint.west.rawValue // sunsetDirection is "west"
let possiblePlanet =Planet(rawValue: 7) // possiblePlanet is of type Planet? and equals Planet.uranus
let positionToFind =11 iflet somePlanet =Planet(rawValue: positionToFind) { switch somePlanet { case .earth: print("Mostly harmless") default: print("Not a safe place for humans") } } else { print("There isn't a planet at position \(positionToFind)") } // Prints "There isn't a planet at position 11"
递归枚举是一个枚举类型的 case 的关联值是自身的一个实例,需要提示关键词 indirect 告诉编译器递归枚举。下面两种写法都可以。注意关键词的位置。
enumArithmeticExpression{ case number(Int) indirectcase addition(ArithmeticExpression, ArithmeticExpression) indirectcase multiplication(ArithmeticExpression, ArithmeticExpression) }
indirectenumArithmeticExpression{ case number(Int) case addition(ArithmeticExpression, ArithmeticExpression) case multiplication(ArithmeticExpression, ArithmeticExpression) }
对这个枚举的一个应用。
let five =ArithmeticExpression.number(5) let four =ArithmeticExpression.number(4) let sum =ArithmeticExpression.addition(five, four) let product =ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))
structSomeStructure{ // structure definition goes here } classSomeClass{ // class definition goes here }
structResolution{ var width =0 var height =0 } classVideoMode{ var resolution =Resolution() var interlaced =false var frameRate =0.0 var name: String? }
let someResolution =Resolution() let someVideoMode =VideoMode()
print("The width of someResolution is \(someResolution.width)") // Prints "The width of someResolution is 0"
print("The width of someVideoMode is \(someVideoMode.resolution.width)") // Prints "The width of someVideoMode is 0"
someVideoMode.resolution.width =1280 print("The width of someVideoMode is now \(someVideoMode.resolution.width)") // Prints "The width of someVideoMode is now 1280"
按成员初始化一个结构体。
let vga =Resolution(width: 640, height: 480)
枚举类型和结构体传递值,而类传递引用。
enumCompassPoint{ case north, south, east, west mutatingfuncturnNorth() { self= .north } } var currentDirection =CompassPoint.west let rememberedDirection = currentDirection currentDirection.turnNorth()
print("The current direction is \(currentDirection)") print("The remembered direction is \(rememberedDirection)") // Prints "The current direction is north" // Prints "The remembered direction is west"
let tenEighty =VideoMode() tenEighty.resolution = hd tenEighty.interlaced =true tenEighty.name ="1080i" tenEighty.frameRate =25.0
let alsoTenEighty = tenEighty alsoTenEighty.frameRate =30.0
print("The frameRate property of tenEighty is now \(tenEighty.frameRate)") // Prints "The frameRate property of tenEighty is now 30.0"
用类似 JS 的三等号操作符判断 2 个对象是否引用同一个对象。
if tenEighty === alsoTenEighty { print("tenEighty and alsoTenEighty refer to the same VideoMode instance.") } // Prints "tenEighty and alsoTenEighty refer to the same VideoMode instance."