1.map的使用
用例1
let numArr = [10, 20, 30, 40, 50];
// money为数组中的每一个元素值,Sting为返回类型
// let total = numbers2.map({money->String in
// return String(money) + "元"
// })
// 简写一 $0为数组中的每一个元素值
// let totalNum = numArr.map({"\($0)元"})
// 简写二
// let totalNum = numArr.map{"\($0)元"}
// 简写三
// let totalNum = numArr.map{ return "\($0)元"}
// 简写四
let totalNum = numArr.map({money in
String(money) + "元"
})
print(totalNum)//输出结果:["10元", "20元", "30元", "40元", "50元"]
用例2
let num = [1,2,3,4,5]
// let result = num.map { $0 + 10 }//对数组中的每一个元素都加上10
// let result = num.map { (a) -> Int in
// return a + 10
let result = num.map {
return $0 + 10
}
print(result)//输出结果:[11, 12, 13, 14, 15]
用例3
let arr:[String] = ["iOS","Swift","java","kotlin"]
print(arr)
let result = arr.map(stringlength)
// let result = arr.map { stringlength(string: $0)}
// let result = arr.map({string -> Int in
// return string.count
// })
// let result = arr.map {
// $0.count
// }
print(result)//输出结果:[3, 5, 4, 6]
func stringlength(string: String) -> Int {
return string.count
}
用例4
let str = "1 2 3"
let ar = str.split(separator: " ").map(String.init)// 分割后转换
ar.forEach { (a) in
print(a)
}
有map(String.init)的截图
![图二.png](https://upload-images.jianshu.io/upload_images/1732937-860140253c6c78ad.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
无map(String.init)的截图
![图一.png](https://upload-images.jianshu.io/upload_images/1732937-e431080f843484dc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2.filter的使用
用例1
let numArr = [10,20,30,40,50,60]
let result = numArr.filter({ $0 > 30 })//过滤出大于30的数
let result1 = numArr.filter(filter)
print(result)//输出结果:[40, 50, 60]
func filter(a:Int) -> Bool {
return a>30
}
用例2
let words = ["iOS","swift","java","kotlin"]
let tweet = "This is an swift App and iOS App"
// 找出tweet中 用到words里 的单词,即tweet使用了words里的那个单词
let result = words.filter {
tweet.contains($0)////为True时返回对应的字符串,false时不返回对应的字符串
}
print(result)//输出结果["iOS", "swift"]
3.reduce的使用
let numArr = [10, 20, 30, 40, 50];
// $0为累加的值 ,$1为下一个值,10为初始值。第一次执行$0为初始值10,$1为numArr中的第一个元素10,第二次执行时$0为第一次执行后的结果 20 = 10 + 10,$1为numArr中的第二个元素20
let totalNum = numArr.reduce(10) {//结果为160
$0 + $1
}
// a为累加的值 ,b为下一个值,0为初始值
// let totalNum = numArr.reduce(0) { (a, b) -> Int in
// return a + b
// }//结果为150
// 0为初始值 ,+代表numArr中的元素相加
// let totalNum = numArr.reduce(0, +)//输出结果:150
// 另外一种用法
// 100为传入的初始值,a为传入的初始值100,b为numArr中的每一个元素,即b为10,20,30,40,50
// let totalNum = numArr.reduce(into: 100) { (a, b) in
// addNum(a: a, b: b)//自定义的函数,该函数执行5次
// }
print(totalNum)
let array = ["iOS", "swift", "Java", "kotlin"]
// $0为初始值"" ,
// let str = array.reduce("") {
//// return $0 == "" ? $1 : $0 + "、" + $1//输出:iOS、swift、Java、kotlin
//// $0 + $1//输出:iOSswiftJavakotlin
//// $0 + "、" + $1//输出:、iOS、swift、Java、kotlin
// if $0 == "" {
// return $1
// }
// return $0 + "、" + $1//输出:iOS、swift、Java、kotlin
// }
// let str = array.reduce("") { (a, b) -> String in
// return a == "" ? b : a + "、" + b//输出:iOS、swift、Java、kotlin
// }
//与上面等效
let str = array.joined(separator: "、")////输出:iOS、swift、Java、kotlin
print(str)
4.flatMpa的使用
// 第一个作用:去空 (有空值存在)
// let arr:[String?] = ["iOS","swift","java",nil,"kotlin"]
// print(arr)//输出:[Optional("iOS"), Optional("swift"), Optional("java"), nil, Optional("kotlin")]
// let result = arr.map {$0}//输出结果:[Optional("iOS"), Optional("swift"), Optional("java"), nil, Optional("kotlin")]
// let result = arr.flatMap {$0}//输出结果:["iOS", "swift", "java", "kotlin"] 使用flatMap后,过滤掉了nil,数组中的所有元素都被解包了即把 Optional 解包,并且原数组的类型由[String?]变成了 [String]
// 注意:没有空值存在的情况如下
let arr1:[String] = ["iOS","swift","java","kotlin"]
// let result = arr1.map {$0}//输出结果:["iOS", "swift", "java", "kotlin"]
// let result = arr1.flatMap {$0}//arr1的类型为[String]时输出结果:["i", "O", "S", "s", "w", "i", "f", "t", "j", "a", "v", "a", "k", "o", "t", "l", "i", "n"]。arr1的类型为[String?]时输出结果:["iOS", "swift", "java", "kotlin"]
let result = arr1.compactMap {$0}//arr1的类型为[String]和[String?]时输出结果一样:["iOS", "swift", "java", "kotlin"]
print(result)
// 第二个作用:降维
let numArr = [[1,2,3],[4,5,6]]
// let arr = numArr.map{ $0 }//输出:[[1, 2, 3], [4, 5, 6]]
// let arr = numArr.map {
// $0.map({
// $0 + 10
// })
// }//输出:[[11, 12, 13], [14, 15, 16]]
// let arr = numArr.flatMap{ $0 }//输出:[1, 2, 3, 4, 5, 6]
let arr = numArr.flatMap {
// $0.flatMap({
// $0 + 10
// })//输出:[11, 12, 13, 14, 15, 16]
$0.map({
$0 + 10
})//输出:[11, 12, 13, 14, 15, 16]
}
print(arr)
let array = ["iOS", "swift", "java","", "kotlin"]
let arr12 = array.map { a -> Int? in
let length = a.count
guard length > 0 else { return nil }
return length
}//输出结果:[Optional(3), Optional(5), Optional(4), nil, Optional(6)]
let arr22 = array.flatMap { a-> Int? in
let length = a.count
guard length > 0 else { return nil}
return length
}//输出结果:[3, 5, 4, 6],解包
print(arr12)
print(arr22)
5.compactMap的使用
// 苹果在Swift 4.1中新增compactMap函数,用来代替flatMap函数,compactMap可以去空,但不能降维,从定义可以看出,经过flatMap后一定变成字符串数组,而compactMap是任意类型的数组。从而compactMap使用更加灵活。
let nums = [4, 8, 12, 20, 30,36]
let result1 = nums.compactMap { (item) -> Int? in
if item%3 == 0 {
return item
}
return nil
}
print(result1) //输出结果:[12, 30, 36]
// let nums = [4, 8, nil,12, 20, 30,36,nil]
// let result1 = nums.compactMap {
// $0//输出结果:[4, 8, 12, 20, 30, 36]
// }
let nums = [[6,8,15],[10,20],[15,25,35],[]]
// let result1 = nums.compactMap {
// $0//输出结果:[[6, 8, 15], [10, 20], [15, 25, 35], []]
// }
let result1 = nums.flatMap {
$0//输出结果:[6, 8, 15, 10, 20, 15, 25, 35]
}
print(result1)
6.高阶函数的混合使用
1.组合
// 筛选出能被2整除的数
let numArr2 = [[6,8,15],[10,20],[15,25,35],[]]
// let result2 = numArr2.flatMap {
// $0.filter { $0 % 2 == 0 }//输出结果:[6, 8, 10, 20]
// }
// 求和
let result2 = numArr2.map({ $0.reduce(0, +) })//输出结果:[29, 30, 75, 0]
print(result2)
2.链式组合
// 大于18的数求和
let numArr3 = [10,15,16,20,18,30]
// let result3 = numArr3.filter{$0 > 18}.reduce(0,+) //输出结果:50
// 加2之后能被2整除
let result3 = numArr3.map{$0 + 6}.filter{$0 % 2 == 0} //输出结果:[16, 22, 26, 24, 36]
print(result3)
达则兼济天下