从 0 到 1 实现 Swift App (二)循环控制

swift 中的循环控制语法跟 oc 比较有些不同,循环体可以使用开闭区间来进行控制循环体,穿插使用 switch 进行类型匹配

        // 循环控制
         for _ in 1...5 {
             print("Li")
         }
         
         // 开
         for i in stride(from: 0, to: 50, by: 5) {
             print(i)
         }
         
         // 闭
         for i in stride(from: 0, through: 50, by: 5) {
             print(i)
         }

         // while 循环 使用 repeat
         var count = 0
         repeat {
           print(count)
           count += 1;
         
         }while count < 5
         
         // Switch  fallthrough 保存隐式贯穿
       
         let number = 10
         switch number {
         case 1:
           print("1");
           fallthrough
         case 2:
           print("2");
         //            fallthrough
         case 1...2:
           print("包含");
         //            fallthrough
         default:
           print("other")
         }
         
        
         // 匹配元组
         let ay = (1,1)
         switch ay {
         case (0,1):
         
           print("this is box")
         
         case(_ , 0):
         
           print("this is box")
         
         case(1,0):
         
           print("")
         
         case (1,1):
         
           print("there")
         
           default:
         
           print("other")
         }
        
        
          // switch 绑定值
        
         let ay = (1,1)
         switch ay {
         case (let y,1):
           print(" this is \(y)")
         default:
           print("other")
         }
         
        var number = 10
        whileLoop: while number > 0 {
        
        switch number {
        case 9:
            print("9")
        case 10:
            
            var sum = 0
            for i in 1...10 {
                
                sum += 1;
                if i == 9 {
                    
                    print(sum)
                    break whileLoop
                    
                }
            }
        default:
            break
        }
        
    }
        number -= 1
               
        // guard 只有在条件为假的情况下进入≥
       
        let isDebug = false
        guard isDebug else {
            print("isDebug");
            return
        }
               
        // 类型匹配
        /*
        struct Dog : Animal {
            var name: String {
                return "dog"
            }
            
            var runSpeed: Int
            
        }
        
        struct Bird : Animal {
            var name: String {
                return "bird"
            }
            
            var speed: Int
            
        }
        
        struct Fish : Animal {
            var name: String {
                return "fish"
            }
            
            var depth: Int
            
        }
        
        let animals:[Any] = [Dog(runSpeed: 220),Bird(speed: 220),Fish(depth: 1200)]
            
        for animal in animals {
            switch animal {
            case let dog as Dog:
                
                print("dog\(dog.runSpeed)")
                
            case let bird as Bird:
                
                print("bird \(bird.speed)")
                
            case let fish as Fish:
                
                print(fish.depth)
                
            default:
                break;
            }
        }
                
        let teacher = Teacher(salary: 1000, name: "jonny")
        
        switch teacher {
        case 0..<2000:
            
            print("温饱")
            
        case 2000..<5000:
            
            print("小康")
            
        case 5000..<10000:
            
            print("滋润")
        default:
            break
        }
    

你可能感兴趣的:(从 0 到 1 实现 Swift App (二)循环控制)