Chinaunix首页 | 论坛 | 博客
  • 博客访问: 257879
  • 博文数量: 42
  • 博客积分: 2415
  • 博客等级: 大尉
  • 技术积分: 590
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-13 14:14
文章分类

全部博文(42)

文章存档

2018年(1)

2017年(8)

2015年(3)

2012年(4)

2011年(11)

2010年(1)

2009年(5)

2008年(9)

我的朋友

分类: iOS平台

2015-12-12 18:14:10


  1. enum SomeEnumeration {
  2.         // enumeration definition goes here
  3.     }



  4. enum CompassPoint {
  5.         case North
  6.         case South
  7.         case East
  8.         case West
  9.     }

  1. enum Planet {
  2.         case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
  3.     }

  1. var directionToHead = CompassPoint.West
  2.     directionToHead = .East

  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"

  1. et 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 UPCA(Int, Int, Int, Int)
  3.         case QRCode(String)
  4.     }

  5. var productBarcode = Barcode.UPCA(8, 85909, 51226, 3)
  6. productBarcode = .QRCode("ABCDEFGHIJKLMNOP")


  7.     switch productBarcode {
  8.     case .UPCA(let numberSystem, let manufacturer, let product, let check):
  9.         print("UPC-A: \(numberSystem), \(manufacturer), \(product), \(check).")
  10.     case .QRCode(let productCode):
  11.         print("QR code: \(productCode).")
  12.     }
  13.     // prints "QR code: ABCDEFGHIJKLMNOP."



  14.     switch productBarcode {
  15.     case let .UPCA(numberSystem, manufacturer, product, check):
  16.         print("UPC-A: \(numberSystem), \(manufacturer), \(product), \(check).")
  17.     case let .QRCode(productCode):
  18.         print("QR code: \(productCode).")
  19.     }
  20.     // prints "QR code: ABCDEFGHIJKLMNOP."

  1. enum ASCIIControlCharacter: Character {
  2.         case Tab = "\t"
  3.         case LineFeed = "\n"
  4.         case CarriageReturn = "\r"
  5.     }

  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.      
  10.     let sunsetDirection = CompassPoint.West.rawValue
  11.     // sunsetDirection is "West"



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



  14.     let positionToFind = 9
  15.     if let somePlanet = Planet(rawValue: positionToFind) {
  16.         switch somePlanet {
  17.         case .Earth:
  18.             print("Mostly harmless")
  19.         default:
  20.             print("Not a safe place for humans")
  21.         }
  22.     } else {
  23.         print("There isn't a planet at position \(positionToFind)")
  24.     }
  25.     // prints "There isn't a planet at position 9"

  1. enum ArithmeticExpression {
  2.         case Number(Int)
  3.         indirect case Addition(ArithmeticExpression, ArithmeticExpression)
  4.         indirect case Multiplication(ArithmeticExpression, ArithmeticExpression)
  5.     }



  6.     indirect enum ArithmeticExpression {
  7.         case Number(Int)
  8.         case Addition(ArithmeticExpression, ArithmeticExpression)
  9.         case Multiplication(ArithmeticExpression, ArithmeticExpression)
  10.     }



  11.     func evaluate(expression: ArithmeticExpression) -> Int {
  12.         switch expression {
  13.         case .Number(let value):
  14.             return value
  15.         case .Addition(let left, let right):
  16.             return evaluate(left) + evaluate(right)
  17.         case .Multiplication(let left, let right):
  18.             return evaluate(left) * evaluate(right)
  19.         }
  20.     }
  21.      
  22.     // evaluate (5 + 4) * 2
  23.     let five = ArithmeticExpression.Number(5)
  24.     let four = ArithmeticExpression.Number(4)
  25.     let sum = ArithmeticExpression.Addition(five, four)
  26.     let product = ArithmeticExpression.Multiplication(sum, ArithmeticExpression.Number(2))
  27.     print(evaluate(product))
  28.     // prints "18"








阅读(730) | 评论(0) | 转发(0) |
0

上一篇:swift笔记-Closures

下一篇:标记一下

给主人留下些什么吧!~~