美文网首页
swift 4.0 枚举

swift 4.0 枚举

作者: 透支未来 | 来源:发表于2018-01-26 11:17 被阅读24次

    枚举

    
    import UIKit
    
    
    indirect enum Arithemtic{
        case number(Int)
        case addition(Arithemtic,Arithemtic)
        case multiplication(Arithemtic,Arithemtic)
    }
    
    //枚举 swift中没有默认的整形值
    enum SomeEnumeration{
        case north
        case south
        case east
        case west
    }
    
    //多个成员值要写在同一行,用逗号隔开
    enum Plant{
        case mercury,earth,mars
    }
    
    //关联值
    enum BarCode {
        case upc(Int,Int,Int,Int) //根据数字识别商品
        case qrCode(String) //根据代码识别商品
    }
    
    //枚举的原始值
    enum ASCIIControlChar : Character{
        case tab = "\t"
        case lineFeed = "\n"
        case carriageReturn = "\r"
    }
    
    //字符串的默认值是枚举值本身  数字依次往后+1
    enum Planet:Int {
        case mercury = 1, venus,earth,mars
    }
    
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            
            
            var directionToHead = SomeEnumeration.west
            directionToHead = .east
            
            switch directionToHead {
            case .north:
                print("north")
            case .south:
                print("south")
            case .east:
                print("east")
            default:
                print("没有这个方向")
            }
            
            //关联值
            var productBar = BarCode.upc(8, 8, 3, 4)
            productBar = .qrCode("ABCDE")
            
            //获取枚举变量的原始值
            let earthsOrder = Plant.earth.hashValue
            print(earthsOrder)
            
            //创建递归表达式
            let five = Arithemtic.number(5)
            let four = Arithemtic.number(4)
            let sum = Arithemtic.addition(five, four)
            let product = Arithemtic.multiplication(sum, five)
            // (5 + 4) * 5
            
            
        }
    }
    
    
    

    相关文章

      网友评论

          本文标题:swift 4.0 枚举

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