美文网首页Swift
Swift循环函数

Swift循环函数

作者: 肆点壹陆 | 来源:发表于2019-03-03 17:37 被阅读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

  • 使用 ..< 指定半开区间,使用 ... 指定闭区间
  • 使用 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来实现循环的操作少之又少,我们只需要熟练使用其他的方式即可

相关文章

  • Swift循环函数

    开发语言:Swift 4.2开发环境:Xcode 10.1 在Swift中,已很少使用for(;;)的循环方式,取...

  • swift - 1

    swift大纲---一天学会swift 简单值:常量,变量 控制流:条件判断语句,循环语句 函数和闭包:函数特殊,...

  • Swift入门二(对象函数)

    函数 闭包 GCD 尾随闭包 闭包循环引用解决(OC) 闭包循环引用解决(swift推荐) 面向对象 命名空间: ...

  • Swift基础-04(函数 闭包)

    1.Swift中函数的使用 函数的定义 外部参数 _的使用 常见的 "_" 在for循环中 函数的默认值 无返回值...

  • swift基础小结1

    swift基础小结 for 循环OC与Swift对比 While循环与downhill循环 OC与swift使用的...

  • Swift 学习笔记(一)

    swift学习笔记第一篇,主要常量,变量,数据类型,条件判断,循环, 函数等基础知识的汇总 大纲汇总 Swift ...

  • Swift7 - 循环、函数

  • Swift for循环与高阶函数

    for in的使用 for in的本质 Swift 的for in使用起来很爽,写法跟其它语言差别还是挺大的,它的...

  • Swift学习笔记(二)

    Swift函数 Swift函数包含参数类型和返回值类型 函数定义 Swift使用关键字func定义函数。 函数定义...

  • Swift学习笔记(三)——循环、函数、类、结构体

    ...为封闭循环,类似<=;..<为半开循环。类似< swift也有oc中的for in循环 swift中用rep...

网友评论

    本文标题:Swift循环函数

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