Swift 流程控制

前端之家收集整理的这篇文章主要介绍了Swift 流程控制前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一.for循环

for-in

实例1:

       for index in 1...5{
            print("\(index)times 5 is \(index * 5)")
        }
结果:
1times 5 is 5
2times 5 is 10
3times 5 is 15
4times 5 is 20
5times 5 is 25
1...5表示:[1,5]大于等于1且小于等于5

如果括号内不使用index,可以用_代替:

 var a = 5
        for _ in 1...5{
            a += 1
        }
        print("\(a)")
结果:
10

遍历数组:
 let nameArr = ["Lili","Marry","xiaoming"]
        for name in nameArr {
            print("Hello,\(name)!")
        }
       
结果:
Hello,Lili!
Hello,Marry!
Hello,xiaoming!

遍历字典:

  let ageDict = ["Lili":9,"Marry":10,"xiaoming":12]
        for (name,age) in ageDict {
            print("\(name) is \(age)!")
        }
结果:
Marry is 10!
Lili is 9!
xiaoming is 12!
由于字典是无序的,所以结果不一定按顺序输出


二。while循环

语法:

    while condition {

        statements

    }

举例:
   //唉,举例无能
        var a = 10
        while a>0 {
            a = a-2
        }
        print(a)
结果:0

原来的do...while循环在此处替换成了repeat...while循环。(不举例了)


三。switch

实例:

        let someCharacter: Character = "z"
        switch someCharacter {
        case "a":
            print("The first letter of the alphabet")
        case "z":
            print("The last letter of the alphabet")
        default:
            print("Some other character")
        }
结果:
The first letter of the alphabet

* 注意:与C和Objective_C不同,3.0的swift中switch语句的case中不再需要使用break跳出switch语句,匹配条件后即跳出Switch语句,同时,switch中的default是必须的。

虽然break在switch中不再是必须的,但是由于switch中的case/default中语句不允许有空语句,此时,我们可以使用break跳出语句。

例如:

    let anotherCharacter: Character = "a"
        switch anotherCharacter {
        case "a": // Invalid,the case has an empty body
        case "A":
            print("The letter A")
        default:
            print("Not the letter A")
        }
        if let integerValue = possibleIntegerValue {
            print("The integer value of \(numberSymbol) is \(integerValue).")
        } else {
            print("An integer value could not be found for \(numberSymbol).")
        }
case "a":是无效的,这将导致程序报错: 'case' label in a 'switch' should have at least one executable statement。

再看下面这个例子
  let numberSymbol: Character = "三"  // Chinese symbol for the number 3
        var possibleIntegerValue: Int?
        switch numberSymbol {
        case "1","١","一","๑":
            possibleIntegerValue = 1
        case "2","٢","二","๒":
            possibleIntegerValue = 2
        case "3","٣","三","๓":
            possibleIntegerValue = 3
        case "4","٤","四","๔":
            possibleIntegerValue = 4
        default:
            break
        }
        if let integerValue = possibleIntegerValue {
            print("The integer value of \(numberSymbol) is \(integerValue).")
        } else {
            print("An integer value could not be found for \(numberSymbol).")
        }
        // Prints "The integer value of 三 is 3."
此时,default中的可能性很多,我们可能不再需要设置一个possibleIntegerValue的值,此时就可以使用break跳出switch语句。

间隔匹配:

例如:

 let grade = 67
        switch grade {
        case 0...59:
            print("不及格")
        case 60...79:
            print("及格")
        case 80...89:
            print("良好")
        case 90...100:
            print("优秀")
        default:
            break
        }
//输出:及格
元祖(Tuples)匹配:
    let somePoint = (1,1)
    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")
    }
    // Prints "(1,1) is inside the Box"

下划线(_)也称为通配符,可以匹配任何可能的值。

值绑定:

    let anotherPoint = (2,0)
    switch anotherPoint {
    case (let x,0):
        print("on the x-axis with an x value of \(x)")
    case (0,let y):
        print("on the y-axis with a y value of \(y)")
    case let (x,y):
        print("somewhere else at (\(x),\(y))")
    }
    // Prints "on the x-axis with an x value of 2"


where:Switch中还可以使用where来设置附加条件

例如:

    let yetAnotherPoint = (1,-1)
    switch yetAnotherPoint {
    case let (x,y) where x == y:
        print("(\(x),\(y)) is on the line x == y")
    case let (x,y) where x == -y:
        print("(\(x),\(y)) is on the line x == -y")
    case let (x,y):
        print("(\(x),\(y)) is just some arbitrary point")
    }
    // Prints "(1,-1) is on the line x == -y"


控制转移语句:

1.continue;

2.break(不再重复说明);

3.fallthrough;

4.return.

5.throw。


continue:跳出当前循环,执行下一次循环。

例如:

    let puzzleInput = "great minds think alike"
    var puzzleOutput = ""
    for character in puzzleInput.characters {
        switch character {
        case "a","e","i","o","u"," ":
            continue
        default:
            puzzleOutput.append(character)
        }
    }
    print(puzzleOutput)
    // Prints "grtmndsthnklk"


fallthrough:条件匹配成功后,继续执行后面的语句。

例如:

    let integerToDescribe = 5
    var description = "The number \(integerToDescribe) is"
    switch integerToDescribe {
    case 2,3,5,7,11,13,17,19:
        description += " a prime number,and also"
        fallthrough
    default:
        description += " an integer."
    }
    print(description)
    // Prints "The number 5 is a prime number,and also an integer."

return:跳出整个循环与语句。 原文链接:https://www.f2er.com/swift/323314.html

猜你在找的Swift相关文章