经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Swift » 查看文章
Swift4 枚举,例子代码
来源:cnblogs  作者:youv  时间:2018/9/25 20:29:17  对本文有异议

枚举

苹果官方文档 枚举

苹果官方文档中文翻译 枚举

枚举语法

  1. enum SomeEnumeration {
  2. // enumeration definition goes here
  3. }
  1. enum CompassPoint {
  2. case north
  3. case south
  4. case east
  5. case west
  6. }
  7. var directionToHead = CompassPoint.west
  8. directionToHead = .east
  1. enum Planet {
  2. case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
  3. }

使用Switch语句来匹配枚举值

  1. directionToHead = .south
  2. switch directionToHead {
  3. case .north:
  4. print("Lots of planets have a north")
  5. case .south:
  6. print("Watch out for penguins")
  7. case .east:
  8. print("Where the sun rises")
  9. case .west:
  10. print("Where the skies are blue")
  11. }
  12. // prints "Watch out for penguins"

如果不能为所有枚举成员都提供一个 case,那你也可以提供一个 default 情况来包含那些不能被明确写出的成员:

  1. let somePlanet = Planet.earth
  2. switch somePlanet {
  3. case.earth:
  4. print("Mostly harmless")
  5. default:
  6. print("Not a safe place for humans")
  7. }
  8. // Prints "Mostly harmless"

关联值

  1. enum Barcode {
  2. case upc(Int, Int, Int, Int)
  3. case qrCode(String)
  4. }
  5. var productBarcode = Barcode.upc(8, 85909, 51226, 3)
  6. productBarcode = .qrCode("ABCDEFGHIJKLMNOP")
  7. switch productBarcode {
  8. case .upc(let numberSystem, let manufacturer, let product, let check):
  9. print("UPC: \(numberSystem), \(manufacturer), \(product), \(check).")
  10. case .qrCode(let productCode):
  11. print("QR code: \(productCode).")
  12. }
  13. // Prints "QR code: ABCDEFGHIJKLMNOP."

如果对于一个枚举成员的所有的相关值都被提取为常量,或如果都被提取为变量,为了简洁,你可以用一个单独的 var或 let在成员名称前标注:

  1. switch productBarcode {
  2. case let .upc(numberSystem, manufacturer, product, check):
  3. print("UPC : \(numberSystem), \(manufacturer), \(product), \(check).")
  4. case let .qrCode(productCode):
  5. print("QR code: \(productCode).")
  6. }
  7. // Prints "QR code: ABCDEFGHIJKLMNOP."

原始值

枚举成员可以用相同类型的默认值预先填充(称为原始值)。

  1. enum ASCIIControlCharacter: Character {
  2. case tab = "\t"
  3. case lineFeed = "\n"
  4. case carriageReturn = "\r"
  5. }

隐式指定的原始值

当你在操作存储整数或字符串原始值枚举的时候,你不必显式地给每一个成员都分配一个原始值。当你没有分配时,Swift 将会自动为你分配值。

  1. enum Planet: Int {
  2. case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
  3. }
  4. enum CompassPoint: String {
  5. case north, south, east, west
  6. }
  7. let earthsOrder = Planet.Earth.rawValue
  8. // earthsOrder is 3
  9. let sunsetDirection = CompassPoint.west.rawValue
  10. // sunsetDirection is "west"

从原始值初始化

用原始值类型来定义一个枚举,那么枚举就会自动收到一个可以接受原始值类型的值的初始化器(叫做rawValue的形式参数)然后返回一个枚举成员或者 nil

  1. let possiblePlanet = Planet(rawValue: 7)
  2. // possiblePlanet is of type Planet? and equals Planet.Uranus

原始值初始化器是一个可失败初始化器

  1. let positionToFind = 11
  2. if let somePlanet = Planet(rawValue: positionToFind) {
  3. switch somePlanet {
  4. case .earth:
  5. print("Mostly harmless")
  6. default:
  7. print("Not a safe place for humans")
  8. }
  9. } else {
  10. print("There isn't a planet at position \(positionToFind)")
  11. }
  12. // Prints "There isn't a planet at position 11"

递归枚举

递归枚举是拥有另一个枚举作为枚举成员关联值的枚举。在声明枚举成员之前使用indirect关键字来明确它是递归的。

  1. enum ArithmeticExpression {
  2. case number(Int)
  3. indirect case addition(ArithmeticExpression, ArithmeticExpression)
  4. indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
  5. }

可以在枚举之前写==indirect==来让整个枚举成员在需要时可以递归:

  1. indirect enum ArithmeticExpression {
  2. case number(Int)
  3. case addition(ArithmeticExpression, ArithmeticExpression)
  4. case multiplication(ArithmeticExpression, ArithmeticExpression)
  5. }
  1. let five = ArithmeticExpression.number(5)
  2. let four = ArithmeticExpression.number(4)
  3. let sum = ArithmeticExpression.addition(five, four)
  4. let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))
  1. func evaluate(_ expression: ArithmeticExpression) -> Int {
  2. switch expression {
  3. case let .number(value):
  4. return value
  5. case let .addition(left, right):
  6. return evaluate(left) + evaluate(right)
  7. case let .multiplication(left, right):
  8. return evaluate(left) * evaluate(right)
  9. }
  10. }
  11. print(evaluate(product))
  12. // Prints "18"
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号