Swift循环函数

开发语言:Swift 4.2
开发环境:Xcode 10.1

在Swift中,已很少使用for(;;)的循环方式,取代的是以下多种循环方式

1、使用for-in循环一个范围

1.1、顺序循环

for index in 0 ..< 5 {
    print(index)
}

输出:
0
1
2
3
4

1.2、逆序循环

for index in (0 ... 5).reversed() {
    print(index)
}

输出:
5
4
3
2
1
0

  • 使用 ..< 指定半开区间,使用 ... 指定闭区间
  • 使用 range.reversed()来指定逆序循环
  • 如果在循环体中,不需要使用index,则可以用 _ 替换 index

2、使用for in循环一个队列

2.1 顺序循环

let test = [10,24,33,6,18]
for value in test {
    print(value)
}

输出:
10
24
33
6
18

2.2 逆序循环

let test = [10,24,33,6,18]
for value in test.reversed() {
    print(value)
}

输出:
18
6
33
24
10

2.3 同时输出下标和值

let test = [10,24,33,6,18]
for (index,value) in test.enumerated() {
    print(index,value)
}

输出:
0 10
1 24
2 33
3 6
4 18

  • 引出问题,混合使用 reversed 与 enumerated 会有怎样的情况
  • test.enumerated().reversed()
  • 依次输出 (4 18) 至 (0...10)
  • test.reversed().enumerated()
  • 依次输出 (0 18) 至 (4...10)

3、使用Array.forEach函数

let test = [10,24,33,6,18]
test.forEach { (value) in
    print(value)
}

输出:
10
24
33
6
18

  • 使用for-in循环时,在循环体内部调用return会直接结束循环
  • 使用Array.forEach循环时,在闭包内调用return只会结束一次闭包调用

4、使用Strideable协议

我们可以实现Strideable协议,也可以使用Strideable协议中stride方法直接进行循环操作

4.1、使用stride(from,to,by)

顺序循环0至10(不包括10),依次递增2

for index in stride(from: 0, to: 10, by: 2) {
    print(index)
}

输出:
0
2
4
6
8

4.2、使用stride(from,through,by)

逆序循环,10至0(包括0),依次递减2

for index in stride(from: 10, through: 0, by: -2) {
    print(index)
}

输出:
10
8
6
4
2
0

4.3、使用Strideable协议

通常使用stride时,我们传递的参数均为Int,当我们想对自己定义的类型进行循环时,这样的方式并不方便,好在我们可以使用Strideable协议解决这个问题

首先我们定义一个类,代表素数

class Prime {
    public var value:Int = 0
    public init(_ v:Int) {
        value = v
    }
}

然后实现Strideable协议

  • func distance(to other: Prime) 表示两个素数之间素数的个数
  • func advanced(by n: Int) -> Prime 返回第n个素数
  • 注意,此函数需要返回一个新的Prime,不能修改自身的值
final class Prime : Strideable {

    //协议
    func distance(to other: Prime) -> Int {
       var count = 0
       for index in value ..< other.value {
           if Prime(index).isPrime() {
               count += 1
            }
       }
       return count
   }
    //协议
   func advanced(by n: Int) -> Prime {
       let result = Prime(value)
       var count = n
       while true {
           result.value += 1
           if result.isPrime() {
               count -= 1
           }
           if count == 0 {
               break
           }
       }
       return result
   }
   
   typealias Stride = Int

   public var value:Int = 0
   public init(_ v:Int) {
       value = v
   }
    //判断当前值是否为素数
   private func isPrime()->Bool {
       guard 2 <= value/2 else {
           return true
       }
       return !((2 ... value/2).contains { (v) -> Bool in
           return value % v == 0
       })
   }
}

使用stride输出给定两个素数之间的所有素数

let start = Prime(2)
let end = Prime(11)
for value in stride(from:start, through: end, by: 1) {
    print(value.value)
}

输出:
2
3
5
7
11

当然,在实际使用中,需要直接继承Strideable来实现循环的操作少之又少,我们只需要熟练使用其他的方式即可

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容