Swift 高阶函数使用
map
map就是是对原对象所有元素进行一对一转换处理,中间不会跳过或遗漏,包括nil元素。
使用
let numList = [1,2,3]
// $0 就是 numList 里的元素
let res = numList.map{$0 + 1}
print("map res = \(res)")
返回
map res = [2, 3, 4]
传入方法
var mapList = ["1","2","3"]
func mapStrDemo(str: String) -> String
{
return str + ": map string"
}
func mapListDemo()
{
let list = mapList.map(mapStrDemo)
print("map list = \(list)")
}
返回
map list = ["1: map string", "2: map string", "3: map string"]
flatMap
flatMap也是可以对原对象元素进行处理。
使用
let numList = [1,2,3]
let res = numList.flatMap{$0 + 1}
print("flatMap res = \(res)")
返回
flatMap res = [2, 3, 4]
传入方法
var flatMapList = ["1","2","3"]
func flatMapStrDemo(str: String) -> String
{
return str + ": flatMap string"
}
func flatMapListDemo()
{
let list = mapList.flatMap(mapStrDemo)
print("flat map list = \(list)")
}
返回
flat map list = ["1: map string", "2: map string", "3: map string"]
compactMap使用
使用
let numList = [1,2,3]
let res = numList.compactMap{$0 + 1}
print("compactMap res = \(res)")
返回
compactMap res = [2, 3, 4]
传入方法
var compactMapList = ["1","2","3"]
func compactMapStrDemo(str: String) -> String
{
return str + ": compactMap string"
}
func compactMapListDemo()
{
let list = compactMapList.compactMap(compactMapStrDemo)
print("compactMap map list = \(list)")
}
返回
compactMap map list = ["1: compactMap string", "2: compactMap string", "3: compactMap string"]
map和flatMap和compactMap 区别
map是对原对象所有元素进行一对一转换处理,中间不会跳过或遗漏,包括nil元素。
flatMap会将二维数组降成一维数组,会将nil去掉。Swift 4.1 flatMap已弃用。
compactMap和flatMap做一样的事情。
// MARK: map 和 flatMap 和 compactMap区别
func mapAndFlatMapAndCompactMapDiffent ()
{
// flatMap 和 compactMap 会将二维数组降成一维数组,
let numList = [[1,2,3],[4,5,6]];
let res = numList.flatMap { $0.map{ $0 + 2 } }
let numList1 = [[1,2,3],[4,5,6]];
let res1 = numList1.map { $0.map{ $0 + 2 } }
let numList2 = [[1,2,3],[4,5,6]];
let res2 = numList2.compactMap { $0.map{ $0 + 2 } }
print("flatMapList = \(res) \nmapList = \(res1) \ncompactMapList = \(res2)")
// flatMap 和 compactMap 会将nil去掉
let strList: [String?] = ["AA", nil, "BB", "CC"]
let res3 = strList.flatMap{ $0 }
let strList1: [String?] = ["AA", nil, "BB", "CC"]
let res4 = strList1.map{ $0 }
let strList2: [String?] = ["AA", nil, "BB", "CC"]
let res5 = strList2.compactMap{ $0 }
print("flatMapStrList = \(res3) \nmapStrList = \(res4) \ncompactMapList = \(res5)")
}
返回
flatMapStrList = ["AA", "BB", "CC"]
mapStrList = [Optional("AA"), nil, Optional("BB"), Optional("CC")]
compactMapList = ["AA", "BB", "CC"]
reduce使用
reduce是累加器,对数据元素进行累计操作
使用
let list = [1,2,3,4,5,6,7,8,9,10]
// 0 为初始的数字 这里从0开始加
// $0 是累加的值 比如说开始的时候是1,1+2之后是3, 3+3之后是6
// $1 是后一个元素,比如说开始的时候是2,1+2之后是3,3+3之后是4
let res = list.reduce(0) {
$0 + $1
}
print("reduce list = \(res)")
返回
reduce list = 55
filter使用
filter:过滤,可以对数组中的元素按照某种规则进行过滤
使用
let list = [1,2,3,4,5,6,7,8,9,10]
let res = list.filter{
$0 > 8
}
print("filter list = \(res)")
返回
filter list = [9, 10]
shuffled使用
shuffled:是对数组进行随机
使用
let list = [1,2,3,4,5,6,7,8,9,10]
let res = list.shuffled()
print("shuffled list = \(res)")
返回
shuffled list = [10, 7, 6, 5, 2, 4, 3, 8, 9, 1]
prefix 使用
prefix 是截取前几个元素
使用
let list = [1,2,3,4,5,6,7,8,9,10]
let res = list.prefix(3)
print("prefix res = \(res)")
返回
prefix res = [1, 2, 3]
suffix 使用
suffix 是截取后几个元素
使用
let list = [1,2,3,4,5,6,7,8,9,10]
let res = list.suffix(3)
print("suffix res = \(res)")
返回
suffix res = [8, 9, 10]
Github地址
懒得打的可以直接打开项目查看 Github地址 https://github.com/HahnLoving/iOS_Study