你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> Swift_控制流

Swift_控制流

編輯:IOS開發綜合

在這裡將為大家介紹的是Swift_控制流的相關介紹,具體代碼請看下文

Swift_控制流

點擊查看源碼

for-in 循環
//for-in 循環
fileprivate func testForIn() {
    //直接循環提取內部數據
    //[1,5]
    for index in 1...5 {
        print("\(index) times 5 is \(index * 5)")
    }
    
    //不需要提取數據 只需要循環次數
    let base = 2
    let power = 3
    var answer = 1
    for _ in 1...power {
        answer *= base
    }
    print("\(base) to the power of \(power) is \(answer)")
    
    //array遍歷
    let array = ["XuBaoAiChiYu", "1045214799"]
    for item in array {
        print("array:\(item)!")
    }
    
    //Dictionary遍歷
    let dict = ["name":"XuBaoAiChiYu", "QQ":"1045214799"]
    for (key, value) in dict {
        print("key:\(key);value:\(value)")
    }
    
    /*  print
     
     1 times 5 is 5
     2 times 5 is 10
     3 times 5 is 15
     4 times 5 is 20
     5 times 5 is 25
     2 to the power of 3 is 8
     array:XuBaoAiChiYu!
     array:1045214799!
     key:name;value:XuBaoAiChiYu
     key:QQ;value:1045214799
     
     */
}
while循環
//while循環
fileprivate func testWhile() {
    //先執行while條件判斷 後執行內部代碼
    let count = 4
    var index = 0
    while index < count {
        index += 1
        print("while:\(index)")
    }
    
    /*  print
     
     while:1
     while:2
     while:3
     while:4
     
     */
}
repeat-while循環
//repeat-while循環
fileprivate func testRepeatWhile() {
    //執行內部代碼後判斷條件
    let count = 4
    var index = 0
    repeat {
        index += 1
        print("RepeatWhile:\(index)")
    } while index < count
    
    /*  print
     
     RepeatWhile:1
     RepeatWhile:2
     RepeatWhile:3
     RepeatWhile:4
     
     */
}
if 判斷
//if 判斷
fileprivate func testIf() {
    //一個條件一個條件的判斷 當條件為真時 執行內部程序
    let temp = 90
    if temp <= 32 {
        //不執行
    } else if temp >= 86 {
        print("執行")
    } else {
        //不執行
    }
    
    /*  print
     
     執行
     
     */
}
swich判斷
//swich判斷
fileprivate func testSwitch() {
    //基本switch (case不會穿透)
    
    let someCharacter: Character = "a"
    switch someCharacter {
    case "a":
        print("1")
    case "a", "b", "c":
        print("2") //這裡不會輸出 case找到後 執行完畢就返回(如果需要穿透 加 fallthrough)
    default:
        print("未找到")
    }
    
    /*  print
     
     1
     
     */
}
switch范圍選擇
//switch范圍選擇
fileprivate func testSwitchIntervalMatching() {
    //范圍選擇
    let approximateCount = 2
    switch approximateCount {
    case 1..<5:
        print("[1, 5)")
    case 5..<10:
        print("[5, 10)")
    default:
        print("未找到")
    }
    
    /*  print
     
     [1, 5)
     
     */
}
switch元組選擇
//switch元組選擇
fileprivate func testSwitchTuples() {
    //元組選擇 坐標軸測試
    let random = arc4random()//隨機數
    let somePoint = (Int(random%3), Int(random%3))//隨機數獲取點
    switch somePoint {
    case (0, 0):
        print("(0, 0) is at the origin")
    case (_, 0):
        print("(\(somePoint.0), 0) is on the x-axis")
    case (0, _):
        print("(0, \(somePoint.1)) is on the y-axis")
    case (-2...2, -2...2):
        print("(\(somePoint.0), \(somePoint.1)) is inside the box")
    default:
        print("(\(somePoint.0), \(somePoint.1)) is outside of the box")
    }
    
    /*  print
     
     (2, 2) is inside the box
     
     */
}
switch值選擇
//switch值選擇
fileprivate func testSwitchValueBindings() {
    let random = arc4random()//隨機數
    //值綁定 如果設置未知數 當匹配成功時 執行此代碼
    let anotherPoint = (Int(random%3), Int(random%1))
    switch anotherPoint {
    case (let x, 0):
        print("\(x),x匹配")
    case (0, let y):
        print("\(y),y匹配")
    case let (x, y):
        print("(\(x), \(y)),其他")
    }
    
    /*  print
     
     1,x匹配
     
     */
}
switch值綁定和where
//switch值綁定和where
fileprivate func testSwitchValueBindingsWhere() {
    //使用where條件二次判斷
    let random = arc4random()//隨機數
    let yetAnotherPoint = (Int(random%3), Int(random%3))
    switch yetAnotherPoint {
    case let (x, y) where x < y:
        print("\(x) < \(y)")
    case let (x, y) where x > y:
        print("\(x) > \(y)")
    case let (x, y):
        print("\(x) = \(y)")
    }
    
    /*  print
     
     1 = 1
     
     */
}
continue
//continue
fileprivate func testContinue() {
    //跳過本次循環 繼續執行下次循環
    for index in 1...5 {
        if index == 2 {
            continue
        }
        print("continue:\(index)")
    }
    
    /*  print
     
     continue:1
     continue:3
     continue:4
     continue:5
     
     */
}
break
//break
fileprivate func testBreak() {
    
    // 跳過當前for或者switch 繼續執行
    for x in 1...5 {
        if x == 2 {
            break
        }
        print("break-x:\(x)")
    }
    print("break-for")
    
    let z = 0
    switch z {
    case 0:
        break;
    default:
        break;
    }
    print("break-switch")
    
    /*  print
     
     break-x:1
     break-for
     break-switch
     
     */
}
fallthrough
//fallthrough
fileprivate func testFallthrough() {
    //擊穿:執行當前case內的代碼 並執行下一個case內的代碼
    let x = Int(arc4random()%1)//0
    switch x {
    case 0:
        print("0")
        fallthrough
    case  1:
        print("1")
        fallthrough
    default:
        break;
    }
    
    /*  print
     
     0
     1
     
     */
}
標簽
//標簽
fileprivate func testLabeledStatements() {
    //標簽語句 可以直接跳到寫標簽行的代碼
    var b = false
    go : while true {
        print("go")
        switch b {
        case true:
            print("true")
            break go //跳過go標簽的代碼
        case false:
            print("false")
            b = true
            continue go //繼續執行go標簽的代碼
        }
    }
    
    /*  print
     
     go
     false
     go
     true
     
     */
}
提前退出
//提前退出
fileprivate func testEarlyExit() {
    //guard和if很像 當條件判斷為假時 才執行else中的代碼
    func greet(_ person: [String: String]) {
        guard let name = person["name"] else {
            print("no name")
            return
        }
        print("name: \(name)!")
    }
    
    greet([:])
    greet(["name": "XU"])
    
    /*  print
     
     no name
     name: XU!
     
     */
}
檢查API可用性
//檢查API可用性
fileprivate func testCheckingAPIAvailability() {
    if #available(IOS 9.1, OSX 10.10, *) {
        print("IOS 9.1, OSX 10.10, *")
    } else {
        print("低版本")
    }
    
    /*  print
     
     IOS 9.1, OSX 10.10, *
     
     */
}



通本學習您是不是更了解ios開發了呢.感謝關注本站

【Swift_控制流】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved