Swift

Swift循环函数

2019-03-03  本文已影响0人  肆点壹陆

开发语言: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

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

3、使用Array.forEach函数

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

输出:
10
24
33
6
18

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协议

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来实现循环的操作少之又少,我们只需要熟练使用其他的方式即可

上一篇 下一篇

猜你喜欢

热点阅读