美文网首页Swift
Swift Enumeration(斯威夫特枚举)

Swift Enumeration(斯威夫特枚举)

作者: Air_w | 来源:发表于2019-06-03 15:52 被阅读0次

    Swift Enumeration(斯威夫特枚举)

    目录:
    1、Enumeration Syntax(枚举语法)
    2、Matching Enumeration value with a switch statement(使用switch语法枚举值)
    3、Iterating over Enumeration cases(迭代枚举实例)
    4、Associated values(关联值)
    5、Raw Values(原始值)
    6、Recursive Enumeration(递归枚举)

    1、Enumeration Syntax(枚举语法)

    Keyword:
    enum define enum class
    case define enum variable

    
    /*
     Define class of enum type.
     */
    enum DayWeek{
        
        case Sunday
        case Monday
        case Tuesday
        case Wendensday
        case Thursday
        case Friday
        case Saturday
        
    }
    
    

    2、Matching Enumeration value with a switch statement(使用switch语法枚举值)

    /*
     Define class of enum type.
     */
    enum DayWeek{
        
        case Sunday
        case Monday
        case Tuesday
        case Wendensday
        case Thursday
        case Friday
        case Saturday
        
    }
    
    /*
     Using switch to match enum type.
     */
    var weekDay = DayWeek.Friday
    
    switch  weekDay{
        case .Monday://be equal to DayWeek.Monday
            print("Monday")
        case .Friday://be equal to DayWeek.Friday
            print("Friday")
        default:
            print("unknown")
    }
    
    

    3、Iterating over Enumeration cases(迭代枚举实例)

    :CaseIterable 启用拥有该枚举的所有实例的集合
    并,将所有实例的集合作为枚举类型的allCases属性公开

    
    /*
     Define class of enum type.
     */
    enum DayWeek : CaseIterable{
        
        case Sunday
        case Monday
        case Tuesday
        case Wendensday
        case Thursday
        case Friday
        case Saturday
        
    }
    
    
    print("enum size:\(DayWeek.allCases.count)")//enum size:7
    
    /*
     Iterate collection of enum.
     */
    for value in DayWeek.allCases{
        print("DayWeek vlaue:\(value)")
    }
    
    
    

    4、Associated values(关联值)

    
    /*
     Define enum type's class
     */
    enum Barcode{
        case productOne(Int,Int)
        case qrCode(String)
        
        case Friday
    }
    
    var productBarcode = Barcode.productOne(8, 8)
    
    productBarcode = Barcode.qrCode("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
    
    //switch expression
    switch productBarcode {
        case .productOne(let one,  let two)://let two never used ,let two equal to _
            print("product:\(one)")
        case .qrCode(let name):
            print("qrCode:\(name)")
        default:
            print("unknown")
    }
    
    /*
     print :qrCode:ABCDEFGHIJKLMNOPQRSTUVWXYZ
     */
    
    
    

    上面可简洁/省略的代码,如下

    未省略/简洁

    //switch expression
    switch productBarcode {
        case .productOne(let one,  let two)://let two never used ,let two equal to _
            print("product:\(one)")
        case .qrCode(let name):
            print("qrCode:\(name)")
        default:
            print("unknown")
    }
    

    省略/简洁

    //switch expression
    switch productBarcode {
        case let .productOne(one,two)://let two never used ,let two equal to _
            print("product:\(one)")
        case let .qrCode(name):
            print("qrCode:\(name)")
        default:
            print("unknown")
    }
    
    

    5、Raw Values(原始值)

    原始值也称为默认值

    
    /*
     Enum default variable value.
     */
    
    enum EnumDefaultValue: Int{
    
        case one = 1
        case two = 2
        
    
    }
    print("enum one value:\(EnumDefaultValue.one)")//enum one values:one
    print("enum one's rawValue:\(EnumDefaultValue.one.rawValue)")//enum one's rawValue:1
    
    
    

    6、Recursive Enumeration(递归枚举)

    定义可递归的枚举(indirect)

    
    enum Expression{
        
        case number(Int)
        
        indirect case addition(Expression,Expression)
        
        indirect case multiplication(Expression, Expression)
    }
    
    func evaluate(_ expression:Expression) -> Int{
        
        switch expression {
        case let .number(value):
            return value
        case let .addition(one, two):
            return (evaluate(one) + evaluate(two))
        case let .multiplication(one, two):
            return (evaluate(one) * evaluate(two))
        default:
            return -1;
        }
    }
    
    var expressionNumber = Expression.number(5)
    print("evaluate:\(evaluate(expressionNumber))")//evaluate:5
    
    var expressionVar = Expression.addition(expressionNumber, expressionNumber)
    print("evaluate:\(evaluate(expressionVar))")//evaluate:10
    
    expressionVar = Expression.multiplication(expressionNumber, expressionNumber)
    print("evaluate:\(evaluate(expressionVar))")//evaluate:25
    
    
    

    想要查看更多、更详细有料干货点击我https://pdliuw.github.io/

    未完待续。。。

    相关文章

      网友评论

        本文标题:Swift Enumeration(斯威夫特枚举)

        本文链接:https://www.haomeiwen.com/subject/bbpatctx.html