Swift 高階函數
作為iOS開發者,我們必須了解高階函數。愛掏網 - it200.com在我們的代碼中使用高階函數可以提高代碼的執行速度,加快我們的開發技巧。愛掏網 - it200.com高階函數可以定義為接受一個或多個函數作為參數并返回一個函數作為結果的函數。愛掏網 - it200.com
在本文中,我們將討論一些Swift的高階函數,包括forEach、map、CompactMap、flatMap、filter、reduce、sort和sorted。愛掏網 - it200.com
forEach函數
forEach函數遍歷數組的所有元素,并且不向編譯器返回任何內容。愛掏網 - it200.com考慮以下示例,我們使用forEach函數遍歷一個整數數組。愛掏網 - it200.com
let marks = [10,20,21,65,32,21]
//1st
marks.forEach{(marks) in
debugPrint(marks, terminator:" ")
}
debugPrint()
//2nd
marks.forEach{ debugPrint($0, terminator:" ")}
在控制臺上打印以下輸出。愛掏網 - it200.com
10 20 21 65 32 21
10 20 21 65 32 21
在第一個語句中,我們使用了forEach()的基本語法來打印數組整數。愛掏網 - it200.com在第二個語句中,我們使用了forEach()的簡寫語法。愛掏網 - it200.comforEach()的工作方式類似于for-in循環。愛掏網 - it200.com然而,我們不能使用break和continue語句來退出forEach中的閉包。愛掏網 - it200.com
map
map用于迭代所有數組元素,并在對每個元素進行一些操作后返回修改后的數組。愛掏網 - it200.com請考慮以下示例,其中我們有一個包含名字的數組,其中包括名字和姓氏。愛掏網 - it200.com我們使用map函數將每個名字中的姓氏分離出來,并將其重新賦值給names。愛掏網 - it200.com
var names = ["John K", "David Garner", "Mike Smith", "Will lee"]
let firstNames = names.map{(name) -> String in
name.split(separator: " ").first!.description
}
debugPrint(firstNames)
let newNames = names.map{$0.split(separator: " ").first!.description}
debugPrint(newNames)
它在控制臺上打印以下輸出。愛掏網 - it200.com
輸出
["John", "David", "Mike", "Will"]
["John", "David", "Mike", "Will"]
compactMap
compactMap迭代數組并返回一個更新的數組,其中只包含滿足compactMap主體內部條件的元素。愛掏網 - it200.com不滿足條件的元素將被排除在修改后的數組之外。愛掏網 - it200.com
考慮以下示例,其中我們有一個包含分數的數組。愛掏網 - it200.comcompactMap()返回更新后的數組,其中包括那些可以轉換為整數的分數。愛掏網 - it200.com
var marks = ["ten ", "10", "20", "thrty one"]
intMarks = marks.compactMap{(marks) in
Int(marks)?.description
}
debugPrint(intMarks)
newMarks = marks.compactMap{Int($0)?.description}
debugPrint(newMarks)
它在控制臺上打印以下輸出。愛掏網 - it200.com
["10", "20"]
["10", "20"]
flatMap
flatMap函數用于將一個二維數組轉換為一個一維數組。愛掏網 - it200.com考慮以下示例。愛掏網 - it200.com
var arrayOfNames = [["John", "Rockey", "David"],
["Smith","Roy","Max"]]
let names = arrayOfNames.flatMap{name in
name
}
debugPrint(names)
let arrnames = arrayOfNames.flatMap{$0}
debugPrint(arrnames)
它在控制臺上打印以下輸出。愛掏網 - it200.com
["John", "Rockey", "David", "Smith", "Roy", "Max"]
["John", "Rockey", "David", "Smith", "Roy", "Max"]
過濾器
正如其名稱所示,filter()函數用于根據特定條件對數組進行過濾。愛掏網 - it200.com它遍歷數組并返回一個經過修改的數組,其中元素滿足過濾器內部的條件。愛掏網 - it200.com
考慮以下示例,在其中我們分別過濾數組中的偶數和奇數。愛掏網 - it200.com
var numbers = [1,2,3,4,5,6,7,8,9,10,11,12]
let evenNumbers = numbers.filter{(num) in
num%2 == 0
}
debugPrint(evenNumbers)
let oddNumbers = numbers.filter{$0%3 == 0}
debugPrint(oddNumbers)
它在控制臺上打印以下輸出。愛掏網 - it200.com
[2, 4, 6, 8, 10, 12]
[3, 6, 9, 12]
reduce
reduce()的作用是遍歷所有數組元素并返回初始結果值和當前結果值的組合對象。愛掏網 - it200.com請考慮以下代碼中使用reduce的語法。愛掏網 - it200.com
reduce(, Result(Result, Int) throws -> Result>)
考慮以下示例,在這個示例中,我們通過將初始結果設置為0來計算數組中所有元素的總和。愛掏網 - it200.com
var numbers = [1,2,3,4,5,6,7,8,9]
let sum = numbers.reduce(0) { (result, num) in
result + num
}
debugPrint(sum)
let s = numbers.reduce(0){0 +1}
debugPrint(s)
sort(by: )和sorted(by: )
sort(by: )和sorted(by: )函數用于根據某些特定條件對元素數組進行排序。愛掏網 - it200.com然而,sorted(by: )會返回一個包含排序后元素的新數組。愛掏網 - it200.com考慮以下示例。愛掏網 - it200.com
var numbers = [10,12,3,14,52,61,17,82,19]
var nums = numbers
//sorted function
let sortedNumbers = numbers.sorted { (a, b) -> Bool in
a<b
}
debugPrint(sortedNumbers)
//sort function
numbers.sort{(a,b) -> Bool in
a<b
}
debugPrint(numbers)
//shorthand syntax of sorted
let sortedNums = numbers.sorted{0<1}
debugPrint(sortedNums)
//shorthand syntax of sort
nums.sort{0<1}
debugPrint(nums)
它在控制臺上打印出以下輸出。愛掏網 - it200.com
輸出
[3, 10, 12, 14, 17, 19, 52, 61, 82]
[3, 10, 12, 14, 17, 19, 52, 61, 82]
[3, 10, 12, 14, 17, 19, 52, 61, 82]
[3, 10, 12, 14, 17, 19, 52, 61, 82]