• 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)
  • 相关阅读:
    Linux磁盘文件的命名
    操作系统的基本介绍
    CMOS、BIOS
    CPU的频率、外频、倍频与超频
    学习Linux——计算机概论
    第三季-第14课-有名管道通讯编程
    第三季-第13课-无名管道通讯编程
    第三季-第12课-多进程程序设计
    第三季-第11课-进程控制理论
    第三季-第10课-时间编程
  • 原文地址:https://www.cnblogs.com/liuguan/p/7506424.html
Copyright © 2020-2023  润新知