swift5.x for-in, switch语句

//
//  ViewController1.swift
//  swiftT
//
//  Created by wjwdive on 2020/5/14.
//  Copyright © 2020 wjwdive. All rights reserved.
//

import UIKit


protocol Animal {
    var name: String {get}
}

struct Dog: Animal {
    var name: String {
        return "Dog"
    }
    var runSpeed : Int;
}

struct Bird: Animal {
    var name: String {
        return "Bird"
    }
    var flightHeight : Int;
}

struct Fish: Animal {
    var name: String {
        return "Fish"
    }
    var depth : Int;
}


class ViewController1: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //for-in 普通遍历
        for i  in 0...3 {
            print(i)
        }
        
        //for-in字典字符串字符
        for c in "hello" {
            print(c)
        }
        
        //for-in数组字符串字符
        let names = ["Jarvis", "Harvis", "Marvis"]
        print("print array: ")
        for name in names {
            print(name)
        }
        
        //for-in字典遍历
        let stuDict = ["name": "Jarvis", "age": 18] as [String : Any]
        print("print dict: ")
        for stu in stuDict {
            print(stu)
        }
        //元组分解字典打印
        for(name, age) in stuDict {
            print("\(name)'s age is \(age)")
        }
        
        //for-in 下划线代替个体
        let base = 3
        let power = 5
        var result = 0
        for _ in 1...power {
            result *= base
        }
        print("\(base) to the power of \(power) is \(result)")
        //间隔循环控制
        //不包括最 to 对应的数字
        print("stride(from:0 to: 50, by: 5)")
        for i in stride(from: 0, to: 50, by: 5) {
            print(i)
        }
        //包括最 through 对应的数字
        print("stride(from:0 through: 50, by: 5)")
        for i in stride(from: 0, through: 50, by: 5) {
            print(i)
        }
        //repeat...while
        
        //OC中 switch 缺少default 可以运行,但是switch 不行,case 对应少了break关键字
        //switch  没有隐式贯穿,需要复合绑定
        //switch  可以进行区间匹配
        //switch 可以进行元组匹配, 用下划线表示匹配所有值
        //switch case 可以 使用 where 子句
        
        let c = "z"
        switch c {
        case "a":
            print("the first letter of alphabet")
        case "z":
           print("the last letter of alphabet")
        default:
            print("other")
        }
        
        let c2 = "a"
        switch c2 {
        case "a", "e", "i", "o", "u":
            print(c2, "是元音字母")
        case "b", "c", "d", "f", "g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z":
            print(c2, "是辅音字母")
        default:
            print(c2, "是其他字符")
        }
        
        //区间匹配
        let count = 62
        switch count {
        case 0:
            print("none")
        case 1...5:
            print("a few")
        case 6..<12:
            print("several")
        case 13..<100:
            print("doznes of")
        case 101..<1000:
            print("handreds of")
        default:
            print("many")
        }
        
        //元组匹配
        let point = (0, 0)
        switch point {
        case (0, 0):
            print("point at origin")
        case (_, 0):
            print("point at x-axis")
        case (0, _):
            print("point at y-axis")
        case (-2...2, -2...2):
            print("point in the box")
        default:
            print("point out of the box")
        }
        
        //where 子句
        let pt = (1, 1)
        switch pt {
        case (let x, let y) where x == y:
            print("point x == y")
        case (let x, let y) where x == -y:
            print("point x == -y")
        default:
            print("other")
        }
        
        
        //控制转移 continue, break, fallthrough, return, throw
        //fallthrough
        let num = 5
        var description = "the number \(num) is"
        switch num {
        case 2, 3, 5, 7, 11, 13, 17:
            description += " a prime number "
        fallthrough
        default:
            description += ", also a prime ineger "
        }
        print(description)
        
        //代码标签
        var number = 10
        whileLoop: while number > 0 {
            switch number {
            case 9:
                print("9")
            case 10:
                var sum = 0
                for i in 0...10 {
                    sum += i;
                    if (i == 9){
                        print(sum)
                        break whileLoop
                    }
                }
            default:
                print("default")
            }
        }
        number = -1
        
        
        
        //模式匹配
        let animals: [Any] = [Dog(runSpeed: 20), Bird(flightHeight: 1000), Fish(depth: 10)]
        
        for animal in animals {
            switch animal {
            case let dog as Dog:
                print("\(dog.name)  run at \(dog.runSpeed)")
            case let fish as Fish:
                print("\(fish.name) swim in \(fish.depth)")
            case is Bird:
                print("bird can fly")
            default:
                break
            }
            
           
        }
        
        
        
    }
    
    
    //guard
    
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

上一篇:swift5.x 多线程的应用场景


下一篇:swift5 接入内购全流程