Swift学习

Swift 3 不常用内置函数介绍

2017-05-23  本文已影响117人  船长_

1 .elementsEqual(other: Sequence)判断两个序列/数组中的元素是否完全相等,元素的顺序也必须相同

示例:

var languages = ["Swift", "Objective-C"]
let flag = languages.elementsEqual(["Swift", "Objective-C"])
print(flag) // true
// 元素的顺序也必须相同
["a","b"].elementsEqual(["b","a"]) == false

2 contains(element: String)序列/数组是否包含某个元素:如果某个序列sequence(比如说一个数组)包含指定的元素element,则返回true,否则返回

false。
写法示例1:

let languages = ["Swift", "Objective-C"]
let flag = languages.contains { (str) -> Bool in
     str == "Java"
}
print(flag) // false

写法示例2:

var languages = ["Swift", "Objective-C"]
languages.contains("Swift") == true
languages.contains("Java") == false

3 .reduce(initialResult: Resul, nextPartialResult: (Result, String) throws -> Result)

给定一个序列sequence,以及一个初始值initial,然后将initial和序列里的第1个元素作为参数传入combineClosure中进行运算,得到的结果保存到initial;然后再将initial和第2个元素传入combineClosure中计算,结果保存到initial;重复计算直到所有sequence中的元素都计算完毕,并返回最终的initial值

写法示例1:

let arr = [1, 2, 4] 
let result = arr.reduce(0) { (result, item) -> Int in
    result + item
}
print(result) // 7

写法示例2:

let languages = ["Swift", "Objective-C"]
let result = languages.reduce("", {$0 + $1})
print(result)  //  SwiftObjective-C

写法示例3:

let arr = [1, 2, 4]
let sum = arr.reduce(0){$0 + $1}
print(sum) // 7

let res = arr.reduce(1){$0 * $1}
print(res)  // 8

4 .joined(separator: String)序列\数组拼接字符串,对数组的元素顺序拼接,并插入指定字符串

示例:

let new = ["A", "B", "C"].joined(separator: ":")
print(new) // A:B:C

5 .filter(isIncluded: (Int) throws -> Bool)序列\数组的元素进行筛选,筛选的结果放入一个新的数组

写法示例1:

let numbers = [10, 1, 3, 4, 5, 1]
let evens = numbers.filter { (a) -> Bool in
      a % 2 == 0
}
print(evens) // [10, 4]

写法示例2:

let numbers = [10, 1, 3, 4, 5, 1]
let evens = numbers.filter{$0 % 2 == 0}
print(evens) // [10, 4]

6 max(x: Comparable, y: Comparable)获取两个元素的最大值,反之,min(x: Comparable, y: Comparable)获取两个元素的最小值

示例:

let a = 11
let b = 22
let maxNum = max(a, b) // 22
let minNum =  min(a, b) // 11

7 arr.max(by: (Int, Int) throws -> Boo) 返回序列/数组中的最大/最小元素

示例:

let arr  = [2, 3, 1,99,4,3]
let maxNum = arr.max { (a, b) -> Bool in
     a < b
 }
print(maxNum) // 99

8 sorted排序,sorted()默认是升序,.sorted(by: (Int, Int) -> Bool)自定义排序

示例1:

let arr  = [2, 3, 1,99,4,3]
let newArr = arr.sorted()
print(newArr) // [1, 2, 3, 3, 4, 99]

示例2自定义降序:

let arr  = [2, 3, 1,99,4,3]
let newArr = arr.sorted { (a, b) -> Bool in
    a > b
}
print(newArr) //[99, 4, 3, 3, 2, 1]

// 简洁写法示例
let newArr = arr.sorted{$0 > $1}

9 map(transform: (Int) throws -> T)对数组/序列中的元素进行特定操作,返回一个新的数组/序列

map函数能够被数组调用,它接受一个闭包作为参数,作用于数组中的每个元素,闭包返回一个变换后的元素,接着将所有这些变换后的元素组成一个新的数组

示例1:数组元素进行2倍放大

let arr = [2,1,3]
let doubleArr = arr.map {$0 * 2}
print(doubleArr) // [4, 2, 6]

示例2:数组中Int转String,并进行2倍放大

let arr = [2,1,3]
let moneyArr = arr.map { "¥\($0 * 2)"}
print(moneyArr) // ["¥4", "¥2", "¥6"]

示例3:数组中的元素转成元组

let arr = [2,1,3]
let groupArr = arr.map {($0, "\($0)")}
print(groupArr) //[(2, "2"), (1, "1"), (3, "3")]

10 flatMap

示例1:flapMap函数会降低维度

let ary  = [["B", "A", "C"],["1","5"]]
let flapMapAry = ary.flatMap{$0}
print(flapMapAry) // ["B", "A", "C", "1", "5"]

示例2:flapMap函数会过滤数组中的 nil

let values: [Int?] = [1, 2, 3, nil, 4]
value.flatMap { $0 }
// 结果是 [1, 2, 3, 4]

11函数round(Double) ceil(Double) floor(Double)

let num : CGFloat = 3.4
// round:如果参数是小数,则求本身的四舍五入
let res = round(num) // 3.0
// ceil:如果参数是小数,则求最大的整数但不大于本身
let res = ceil(num) // 4.0
// floor:如果参数是小数,则求最小的整数但不小于本身
let res = floor(num) // 3.0

关于map 和 flatMap的概念解析Swift 烧脑体操(四) - map 和 flatMap
本文参考以下文章:
Swift常用内置函数介绍
Swift的74个常用内置函数介绍

上一篇下一篇

猜你喜欢

热点阅读