美文网首页
笔记摘要(五)--Swift3.0之控制流

笔记摘要(五)--Swift3.0之控制流

作者: 风御轩 | 来源:发表于2016-10-14 09:34 被阅读11次
    • For循环

      • for-in
        let base = 3
        let power = 10
        var answer = 1
        for _ in 1...power {
        answer *= base
        }
        print("(base) to the power of (power) is (answer)")
      • for 循环
        传统的for循环在swift3.0被取消;i++,++i被取消;
        新的形式:for index in 0..<3 { }
        等价于以前的 for (int index = 0; index < 3; index++){ }
    • While循环

      • while循环,每次在循环开始时计算条件是否符合;
      • repeat-while循环,每次在循环结束时计算条件是否符合。与do-while类似
    • 条件语句

      • if

      • switch

        与OC不同,在swift中,不存在隐式的贯穿,当匹配的case分支中的代码执行完毕后,程序会终止swift语句,而不会继续执行下一个case分支。即不需要在case分支中显式的使用break语句。好处是语句更安全、更易用。
        如果想要贯穿至特定的case分支中,请使用fallthrough语句。
        
    • 控制转移语句

      • continue
        告诉一个循环体立刻停止本次循环迭代,重新开始下次循环迭代。注意:在一个带有条件和递增的for循环体中,调用continue语句后,迭代增量仍然会被计算求值。循环体继续像往常一样工作,仅仅只是循环体中的执行代码会被跳出。
        let puzzleInput = "great minds think alike"
        var puzzleOutput = ""
        for character in puzzleInput.characters {
        switch character {
        case"a", "e", "i", "o", "u", " ":
        continue
        case"b":
        break
        default:
        puzzleOutput.append(character)
        }
        }

        print(puzzleOutput)
        

    输出:
    grtmndsthnklk

    • break
      会立刻结束整个控制流的执行。在swift语句中,你可以使用break来忽略某个分支。

    • fall through
      此关键字 不会检查它下一个将会落入执行的case中的匹配条件。
      let integerToDescribe = 2
      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)
      输出:
      the number 2 is a prime number, and also an integer
      带标签的语句:
      label name: while condition { statements }
      同样的规则适用于所有的循环体和switch代码块。

    • return

    • throw

    • 提前退出

      • 像if语句一样,guard 的执行取决于一个表达式的布尔值。我们可以用来要求条件必须为真时,以执行guard语句后的代码。

      • 不同于if语句,一个guard语句总是有一个else分句,如果条件不为真则执行else分句中的代码。

      • 如果条件不满足,在else分支上的代码就会被执行。这个分支必须转移控制以退出guard语句出现的代码段,它可以用return,break,continue ,throw,或者调用一个不返回的方法或函数,例如:fatalError()。

      • 对比if语句,按需使用guard语句会提升代码的可靠性。它可以使代码连贯的被执行而不需要将它包在else块中,它可以使你处理违反要求的代码使其接近要求。
        func greet(person: [String: String]){
        guard let name = person["name"] else {return}
        print("hello (name)")
        guard let location = person["location"] else {
        print("i hope the weather is nice near you.")
        return
        }
        print("i hope the weather is nice in (location)")}

         greet(person: ["name": "john"])
         greet(person: ["name": "jane", "location": "ShangHai"])
        

    输出:
    hello john
    i hope the weather is nice near you.
    hello jane
    i hope the weather is nice in ShangHai

    • 检测API可用性

      swift有检查API可用性的内置支持。

    相关文章

      网友评论

          本文标题:笔记摘要(五)--Swift3.0之控制流

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