zoukankan      html  css  js  c++  java
  • iOS二次发育(swift)枚举

    //: Playground - noun: a place where people can play
    
    import UIKit
    
    
    //Raw Value
    enum Month: String{
        case January = "Value_January"
        case February = "Value_February"
        case March = "Value_March"
        case April = "Value_April"
        case May = "Value_May"
        case June = "Value_June"
        case July = "Value_July"
        case August = "Value_August"
        case September = "Value_September"
        case October = "Value_October"
        case November = "Value_November"
        case December = "Value_December"
    }
    func season(month: Month ) -> String{
        switch month {
        case .January:
            return month.rawValue
        default:
            return ""
        }
    }
    
    season(month: Month.January)
    print(Month.April)
    
    if let july = Month(rawValue: "Value_July"){
       print(july)
    }
    
    
    //Associate Value
    
    //例1
    enum ATMStatus{
        case Success(Int)
        case Error(String)
        case Waiting
    }
    
    var balance = 1000
    func withdraw( amount: Int ) -> ATMStatus{
        if amount <= balance {
            balance -= amount
            return .Success(balance)
        } else {
            return .Error("Not enough money")
        }
    }
    let result = withdraw(amount: 800)
    switch result {
        case let .Success(newBalance):
            print("(newBalance) Yuan Left in your account.")
        case let .Error(errorMessage):
            print("error:(errorMessage)")
        case .Waiting:
            print("Waiting")
    }
    
    switch result {
        case .Success:
            print("Success")
        case .Error:
            print("Error")
        case .Waiting:
            print("Waiting")
    }
    
    //例2
    enum Shape{
        case Square(side: Double)
        case Rectangle( Double, height: Double)
        case Circle(centerX: Double, centerY: Double, radius: Double)
        case Point
    }
    let square = Shape.Square(side: 10)
    let rectangle = Shape.Rectangle( 20, height: 30)
    let circle = Shape.Circle(centerX: 0, centerY: 0, radius: 15)
    let point = Shape.Point
    
    func area(shape: Shape) -> Double{
        switch shape {
        case let .Square(side):
            return side * side
        case let .Rectangle(width, height):
            return width * height
        case let .Circle(_, _, r):
            return M_PI * r * r
        case .Point:
            return 0
        }
    }
    area(shape: square)
    area(shape: rectangle)
    area(shape: circle)
    area(shape: point)
    var webSite: Optional<String> = Optional.some("123")
    webSite = .none
    
    webSite = "456"
    webSite = nil
    switch webSite {
        case .none:
            print("No webSite")
        case let .some(website):
            print("The website is (website)")
    }
    
    
    indirect enum ArithmeticExpression{
        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 two = ArithmeticExpression.Number(2)
    let product = ArithmeticExpression.Multiplication(sum, two)
    
    func evaluate(expression: ArithmeticExpression) -> Int {
        switch expression {
        case let .Number(value):
            return value
        case let .Addition(left, right):
            return evaluate(expression: left) + evaluate(expression: right)
        case let .Multiplication(left, right):
            return evaluate(expression: left) * evaluate(expression: right)
        }
    }
    evaluate(expression: product)
  • 相关阅读:
    七 HBase表结构设计
    六 一行数据存储到文件的过程。
    五、数据模型特殊属性
    四 数据模型操作
    三、 数据模型概念
    二、 HBase核心功能模块。
    一、 Hbase特性 3v特性,Volume(量级) Varity(种类) Velocity(速度)
    windows下安装redis
    redis缓存穿透和缓存雪崩
    java多线程四种实现方法
  • 原文地址:https://www.cnblogs.com/liuguan/p/7506424.html
Copyright © 2011-2022 走看看