美文网首页
笔记04 swift 枚举

笔记04 swift 枚举

作者: PPFSaber | 来源:发表于2021-04-25 17:02 被阅读0次
    {
            // 枚举的基本用法
            enum PPFDirection {
                case north
                case south
                case east
                case west
            }
            
            enum PPFDirection2 {
                case north,south,east,west
            }
            
            var dir = PPFDirection.west
            dir = PPFDirection.east
            dir = .east
            print(dir)
            
            switch dir {
                case .north:
                    print(dir)
                
                case .west,.east:
                    print(dir)
                
                case .south:
                    print(dir)
            }
            
            //关联值(Associated Values)
            //有时将枚举的成员值跟其他类型的值关联存储在一起,会非常有用
            enum Score {
                case points(Int)
                case grade(Character)
            }
            
            var score = Score.points(96)
            score = .grade("A")
            
            enum Date {
                case digit(year: Int, month: Int, day: Int)
                case string(String)
            }
            
            var date = Date.digit(year: 2011, month: 9, day: 10)
            
            date = .string("2011-09-10")
            
            switch date {
                case.digit(year: 2011, month: 9, day: 10):
                    print("date")
                    
                case .digit(let year, let month, let day):
                    print(year, month, day)
                
                case let .string(value):
                    print(value)
            }
            
            switch score {
                case let .points(i):
                    print(i, "points")
                case let .grade(i):
                    print("grade", i)
            } // grade A
    //        n 必要时let也可以改为var
            
    //        关联值举例 支持多种类型数据判断
            enum Password {
                case number(Int, Int, Int, Int) // 数字组合
                case gesture(String)// 手势数字轨迹
            }
            
            var pwd = Password.number(3, 5, 7, 8)
            pwd = .gesture("12369")
            
            switch pwd {
                case let .number(n1, n2, n3, n4):
                    print("number is ", n1, n2, n3, n4)
                case let .gesture(str):
                    print("gesture is", str)
            }
            
    //        原始值(Raw Values)
    //        n 枚举成员可以使用相同类型的默认值预先对应,这个默认值叫做:原始值
             enum PokerSuit : Character {
                case spade = "♠"
                case heart = "♥"
                case diamond = "♦"
                case club = "♣"
            }
            
            enum Grade : String {
                case perfect = "A"
                case great = "B"
                case good = "C"
                case bad = "D"
            }
            
            print(Grade.perfect.rawValue) // A
            print(Grade.great.rawValue) // B
            print(Grade.good.rawValue) // C
            print(Grade.bad.rawValue) // D
            let suit = PokerSuit.spade
            print(suit) // spade
            print(suit.rawValue) // ♠
            print(PokerSuit.club.rawValue) // ♣
            // 注意:原始值不占用枚举变量的内存
            
    //        隐式原始值(Implicitly Assigned Raw Values)
    //        n 如果枚举的原始值类型是Int、String,Swift会自动分配原始值
            enum PPFDirection3 : String {
                case north = "north"
                case south = "south"
                case east = "east"
                case west = "west"
            }
            // 等价于
            enum PPFDirection4 : String {
                case north, south, east, west
            }
            print(PPFDirection3.north) // north
            print(PPFDirection4.north.rawValue) // north
            enum Season : Int {
                case spring, summer, autumn, winter
            }
            print(Season.spring.rawValue) // 0
            print(Season.summer.rawValue) // 1
            print(Season.autumn.rawValue) // 2
            print(Season.winter.rawValue) // 3
              enum Season2 : Int {
                case spring = 1, summer, autumn = 4, winter
            }
            print(Season2.spring.rawValue) // 1
            print(Season2.summer.rawValue) // 2
            print(Season2.autumn.rawValue) // 4
            print(Season2.winter.rawValue) // 5
             
            enum PPFWord : String {
                case sdf = "a"
                case cd
                case c = "cc"
                case sfsf
                
            }
    
            print(PPFWord.sdf.rawValue)
            print(PPFWord.cd.rawValue)
            print(PPFWord.c.rawValue)
            print(PPFWord.sfsf.rawValue)
            
    //        递归枚举(Recursive Enumeration)  //indirect 间接的
            
            indirect enum ArithExpr {
                case number(Int)
                case sum(ArithExpr, ArithExpr)
                case difference(ArithExpr, ArithExpr)
            }
            
            let dif = ArithExpr.difference(ArithExpr.sum(ArithExpr.number(1), ArithExpr.number(2)), ArithExpr.number(3))
            
            print(dif)
            
            func calculate(_ expr: ArithExpr) -> Int {
                switch expr {
                    case let .number(value):
                        return value
                    case let .sum(left, right):
                        return calculate(left) + calculate(right)
                    case let .difference(left, right):
                        return calculate(left) - calculate(right)
                }
            }
            
    
    
            
            enum ArithExpr2 {
                case number(Int)
                indirect case sum(ArithExpr2, ArithExpr2)
                indirect case difference(ArithExpr2, ArithExpr2)
            }
             let five = ArithExpr.number(5)
            let four = ArithExpr.number(4)
            let two = ArithExpr.number(2)
            let sum = ArithExpr.sum(five, four)
            let difference = ArithExpr.difference(sum, two)
            calculate(difference)
            
    //        n 可以使用MemoryLayout获取数据类型占用的内存大小
            enum PPFPassword {
                case number(Int, Int, Int, Int)
                case other1(Int, Int, Int, Int,Int, Int, Int, Int)
            }
    
             MemoryLayout<PPFPassword>.stride // 40, 分配占用的空间大小
            MemoryLayout<PPFPassword>.size // 33, 实际用到的空间大小
            MemoryLayout<PPFPassword>.alignment // 8, 对齐参数
            var pwd1 = PPFPassword.number(9, 8, 6, 4)
            pwd1 = PPFPassword.other1(1,1,1,123213,1,123,312,13)
           print(MemoryLayout.stride(ofValue: pwd1)) // 40
            print(MemoryLayout.size(ofValue: pwd1)) // 33
            print(MemoryLayout.alignment(ofValue: pwd1)) // 8
            
    //        窥探内存小工具 https://github.com/CoderMJLee/Mems
            
        }
    

    相关文章

      网友评论

          本文标题:笔记04 swift 枚举

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