directionToHead=.SouthswitchdirectionToHead{case.North:println("Lots of planets have a north")case.South:println("Watch out for penguins")case.East:println("Where the sun rises")case.West:println("Where the skies are blue")}
3.相关值
如果能够把其他类型的**相关值**和成员值一起存储起来会很有用。
假设一个库存跟踪系统需要利用两种不同类型的条形码来跟踪商品。有些商品上标有 UPC-A 格式的一维码,它使用数字 0 到 9。每一个条形码都有一个代表“数字系统”的数字,该数字后接 10 个代表“标识符”的数字。最后一个数字是“检查”位,用来验证代码是否被正确扫描。其他商品上标有 QR 码格式的二维码,它可以使用任何 ISO8859-1 字符,并且可以编码一个最多拥有 2,953 字符的字符串。对于库存跟踪系统来说,能够把 UPC-A 码作为三个整型值的元组,和把 QR 码作为一个任何长度的字符串存储起来是方便的。
对于库存跟踪系统来说,能够把 UPC-A 码作为三个整型值的元组,和把 QR 码作为一个任何长度的字符串存储起来是方便的。
switchproductBarcode{case.UPCA(letnumberSystem,letidentifier,letcheck):println("UPC-A with value of \(numberSystem), \(identifier), \(check).")case.QRCode(letproductCode):println("QR code with value of \(productCode).")}
switchproductBarcode{caselet.UPCA(numberSystem,identifier,check):println("UPC-A with value of \(numberSystem), \(identifier), \(check).")caselet.QRCode(productCode):println("QR code with value of \(productCode).")}// 输出 "QR code with value of ABCDEFGHIJKLMNOP."
letpositionToFind=9ifletsomePlanet=Planet(rawValue:positionToFind){switchsomePlanet{case.Earth:println("Mostly harmless")default:println("Not a safe place for humans")}}else{println("There isn't a planet at position \(positionToFind)")}