4.4 Swift中for循环语句

        /**

         for循环

         

         for 初始化语句; 条件; 递增语句

         {

            // 执行指令

         }

         

         */

        

        var sum = 0

        var i = 0

        

     

        /**

         C-style for statement has been removed in Swift 3

         C语言的这种for 循环已经被 swift3.0 移除了

         */

//        for var i = 0; i <= 10; i = i+1

//        {

//            sum += i

//        }

        

        // ,退出条件永远为假

//        for ;;

//        {

//            print("hello world")

//        }

        

        

        // 这个不对

//        for (i, in 10 ){

//            sum += i

//        }

        

        for i in 0...10

        {

            sum += i

        }


        print("--------->")

        print(sum);

        

        

        /**

         Swift 3.0 版本将会去掉沿用已经的 C 风格循环语法, 又是向现代开发语言的一次迈进,

         咱们就来看看没了 C 风格循环我们还有什么选择, 

         看过之后你会不会感觉 C 风格循环在 Swift 中确实有点多余呢?

        

         关于 C 风格循环, 不我们过多介绍了, 就是类似这样的语句:

         let numberList = [1, 2, 3, 4, 5]

         for var i = 0; i < numberList.count; i++ {

         

         }

         

         如今这样的语法在新版本的 Swift 中即将成为历史了,

         C 风格的循环语法可能是大家最熟悉的, 大家会不会觉得突然去掉这个语法有些不适应呢? 

         咱们再来看看 Swift 3 中的替代方案。

         */

        

        


        /**

         for .. in 语法

         第一个替代方案, 我们可以使用 for .. in 这样的语法:

         */

        let numberList = [1,2,3,4,5]

        var result = ""

        for num in numberList {

            result += "\(num) "

        }

        

        print("--------->")

        print("result==\(result)");


        

        

        

        

        /**

         如果我们想知道每次遍历的索引怎么办呢, 还有一种方法:

         num.offset num.element 分别代表对应的索引和元素

         

         enumerate 函数其实就是对 numberList 数组做了一个变换,

         原来它是一个 Int 类型的数组,经过变换后,成为了 (Int, Int) 元组类型的数组

         num 是一个元组。

         */

        for num in numberList.enumerated() {

            print("num.offset==\(num.offset)")

            print("num.element==\(num.element)")

//            result += "[\(num.index)]\(num.element) "

        }



        

        

        

        /**

         上面的num 相当于 (index, value)

         */

        for (index, value) in numberList.enumerated()

        {

            // 这样子遍历,可以得到数组的值和下标

            print("index:\(index) = \(value)")

        }

        

        

        /**

         

         是不是这么回事呢? 查看 enumerate 方法的文档后, 看到它的定义是这样的:

         func enumerate() -> EnumerateSequence<array<element>></array<element>


         

         比我们想象的要复杂些, EnumerateSequence 是个什么鬼, 让我们再来看看它的文档定义:

         

         The SequenceType returned by enumerate(). EnumerateSequence is a sequence of pairs (n, x), where ns are consecutive Ints starting at zero, and xs are the elements of a Base SequenceType

         

         仔细看下, 其实跟我们理解的还是差不多的, 它只不过是对集合类的一个集成, 这个集合每一项是一个元组 (n, x) , n 代表索引, x 代表数组元素。

         */



        


        /**

         那么,我们还可以做点更有意思的事情:

         调用 enumerate 之后再调用 reverse 方法, 我们就可以对一个数组进行反向遍历。

         

         SequenceType

         http://swiftdoc.org/v2.2/protocol/SequenceType

         */

        for (index, item) in numberList.enumerated().reversed() {

            print("index==\(index), \(item)")

        }

        


        print("--------------->")

        for (index, item) in numberList.enumerated().filter({ (index, item) in index % 2 == 0})

        {

            print("index==\(index), \(item)")

        }

        

        


        

        

        

        /**

         区间(Range)循环

         除了刚才咱们说的这些, Swift 还提供了更方便的循环语法, 叫做 Range 循环。 比如这样:

         */

        var rs = ""

        for i in 0...10 {

            rs += "\(i)"

        }

        

        print("--------------->")

        print(rs)

    

        /**

         这个语句会输出 0 10 之间的所有数字,

         0…10 这个表示 Range 区间的范围。 

         当然,对于我们刚才的数组遍历来说, 一般数组索引都是数组长度减去 1

         用这个区间处理起来就会比较麻烦, 不过好在 Swift 给我们提供了另外一种 Range 方法:


         */

        for i in 0...(numberList.count-1)

        {

            print("i===\(i)")

        }

        

        


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值