网站建设资讯

NEWS

网站建设资讯

Swift控制语句-创新互联

条件控制: if 和 Switch

创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站制作、成都网站设计、外贸营销网站建设、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的江城网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

循环控制:for-inforwhile, and do-while

1  参考代码如下:

        let classScore = [100,90,80,70,60,50,40,30]

        var totalScore = 0

        for score in classScore{

          if score >= 60 {

            totalScore += score

          } else {

            println("no pass,score is \(score)" );

          }

        }

        println("totalScore is \(totalScore)");

 2  switch case 循环,不再限定类型:

            let vegetable = "Switch case Type"

            var result = ""

            switch vegetable{

              case "calery":

                 result = "calery"

              case "calendar","china":

                 result = "china"

              case let x where x.hasSuffix("Type"):

                 result = "haSuffix";

              default :

                 result = "default"

            }

            println("result :\(result)")

3  使用for-in 遍历字典:

        let interestingNumbers = [

          "Prime": [2, 3, 5, 7, 11, 13],

          "Fibonacci": [1, 1, 2, 3, 5, 8],

          "Square": [1, 4, 9, 16, 25],

        ]

        var largest = 0

        for (kind, numbers) in interestingNumbers {

          for number in numbers {

            if number > largest {

              largest = number

            }

          }

        }

        println("largest:\(largest)");

4     while 和 do while代码如下:

  • var n = 2

  • while n < 100 {

  •   n = n * 2

  • }

  • var m = 2

  • do {

  •   m = m * 2

  • } while m < 100

5 for循环如下:

                var firstLooper = 0;

                for i in 0..3{

                  println("firstLooper:\(i)")

                  firstLooper += 1;

                }

                var secondLopper = 0;

                for var i = 0;i < 3 ; ++i{

                  println("secondLopper:\(i)")

                  secondLopper += 1;

                }

下一篇文章将讲解方法、函数

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前名称:Swift控制语句-创新互联
本文网址:http://njwzjz.com/article/ddjsep.html