美文网首页
swift 函数

swift 函数

作者: 流星阁 | 来源:发表于2022-10-10 19:54 被阅读0次

1、indices(数组遍历索引)

let list = ["1", "2", "3"]
for index in list.indices {
    print(index)
}
// 0 1 2

2、enumerated (数组遍历索引及元素)

let list = ["A", "B", "C"]
for (index, item) in list.enumerated() {
      print(index, item)
}
// 0 1
// 1 2
// 2 3

3、stride(from: to: by)

//最后一个值小于to的值
for index in stride(from:0, to:12, by:3)  {
    print(index)
}
// print 0, 3, 6, 9
//最后一个值大于to的值
for index in stride(from:12, to:0, by:-3)  {
    print(index)
}
// print 12, 9, 6, 3

4、stride(from: through: by)

//最后一个值小于等于through的值
for index in stride(from: 0, through: 12, by: 3) {
    print(index)
}
// print 0, 3, 6, 9, 12
//最后一个值大于等于through的值
for index in stride(from: 12, through: 0, by: -3) {
    print(index)
}
// print 12, 9, 6, 3, 0

5.map(对集合中的所有元素进行同样的操作,并返回一个新集合)

let nums = [1,2,3]
var mapNums = nums.map {
    $0 + 1
}
print("mapNums:\(mapNums)")
// 2 3 4 

6.flatMap(可将集合中的所有集合取出,最终变成一个新的集合)

let numFlat = [[1,2,3,4],[8,9,7,6]]
let numsFlatMap = numFlat.flatMap {
 $0
}
print("numsFlatMap:\(numsFlatMap)")
// [1,2,3,4,8,9,7,6]

7.CompactMap(当集合中有nil存在时使用CompactMap函数)

let nums = [1,2,nil,3,4,5,6,7,8,nil,nil]
let compactMapNums = nums.compactMap {
    $0
}
print("compactMapNums:\(compactMapNums)")
// [1,2,3,4,5,6,7,8]

8.filter(对集合中的元素进行过滤,返回一个符合条件新的集合)

let nums = [1,2,3,4,5,6,7,8]
var filterNums = nums.filter {
    $0 < 5
}
print("filterNums:\(filterNums)")
// [1,2,3,4]

9.reduce(对集合中所有的元素进行操作,并返回一个新值)

reduce函数传入什么类型就要返回什么类型
reduce函数有两个参数,$0为上一个的结果值,$1为集合中的元素

let nums = [1,2,3,4]
let reduceNums = nums.reduce([Int]()) {
    [$1] + $0
}
print("reduceNums:\(reduceNums)")
// [4,3,2,1]

案例:

// 求数组中元素的平方和
let nums = [1,2,3,4]
let numTemp = nums.filter {
    $0 % 2 == 0
}.reduce(Int()) {
    $0 + $1 * $1
}
print("numTemp:\(numTemp)")
//  20
// 快速排序算法(使用递归)
extension Array where Element: Comparable {
    func quickSorted() -> [Element] {
        if self.count > 1 {
            let (pivot, remaining) = (self[0], dropFirst())
            let lhs = remaining.filter{ $0 <= pivot}
            let rhs = remaining.filter{ $0 > pivot}
            return lhs.quickSorted() + [pivot] + rhs.quickSorted()
        }
        return self
    }
}
print([1,2,33,22,1,2,3].quickSorted())
// [1, 1, 2, 2, 3, 22, 33]

10.dropFirst、dropLast

dropFirst() 除了数组中第一个元素以外的元素
dropFirst(3) 除了数组中第0-3个元素以外的元素
dropLast() 除了数组中最后一个元素以外的元素
dropLast(3) 除了数组中后3个元素(根据需要写)以外的其他元素

let demoAArr = [1,2,3,4]
print(demoAArr.dropFirst())
// [2,3,4] 其他同理

11.数组 removeAll(where:) — 删除

高效根据条件删除,比filter内存效率高,指定不想要的东西,而不是想要的东西,和filter类似的功能。filter把满足条件的返回到新的数组。不是在操作原数组。removeAll(where:)是操作原数组把满足条件的剔除掉。可根据需求合理选择使用filter和removeAll(where:)

var demoAArr = [1,2,3,4]
demoAArr.removeAll(where: { $0 % 2 == 0 })
print(demoAArr)
// [1, 3]

相关文章

  • Swift学习笔记(二)

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

  • Swift中的标准函数

    Swift中的标准函数 Swift中的标准函数

  • Swift の 函数式编程

    Swift の 函数式编程 Swift の 函数式编程

  • 10.函数(function)

    函数 kotlin_函数 swift_函数

  • swift 函数

    Swift 函数 函数声明: 告诉编译器函数的名字,返回类型及参数。 函数定义: 提供了函数的实体。Swift 函...

  • swift学习笔记②

    Swift学习笔记 - 文集 语法篇 一、函数 函数定义 Swift 定义函数使用关键字 func,functio...

  • #6 函数

    swift functions - 极客学院 swift的函数和JS的函数基本类似,有几点不同的是: swift中...

  • iOS知识点-9.Swift 是面向对象还是函数式的编程语言?

    Swift Basics Swift 是面向对象还是函数式的编程语言? Swift既是面向对象的,又是函数式的编程...

  • Swift高阶函数解析

    一、锲子 最近在学习Swift过程中发现,Swift有不少高阶函数,这些函数为Swift支持函数式编程范式提供了强...

  • swift 临时

    //// ViewController.swift// Swift01_函数//// Created by ...

网友评论

      本文标题:swift 函数

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