Map:可以对数组中的每一个元素做一次处理
- 如数组中存在nil,会判断nil,如果存在则返回
override func viewDidLoad() {
super.viewDidLoad()
//可以对数组中的每一个元素做一次处理
let cast = ["Vivien", "Marlon", "Kim", "Karl",nil, nil, "liliguang"]
// map(_ transform: (String) throws -> T) //映射闭包。 transform接受此序列的元素作为其参数,并返回相同或不同类型的转换值。
let lowercaseNames = cast.map { $0.lowercased() } //注意研究各种不同的映射闭包写法
print(lowercaseNames)
let letterCounts = cast.map { $0.count }
print(letterCounts)
print( stringArray.map( lowStr ) )
print( stringArray.map( stringCount ) )
print("------")
print(stringArray.map({ string -> Int in
return string.count
}))
}
//转换为小写
func lowStr(str : String) -> String {
return str.lowercased()
}
//获取字符串长度
func stringCount(string: String) -> Int {
return string.count
}
打印结果
[Optional("vivien"), Optional("marlon"), Optional("kim"), Optional("karl"), nil, nil, Optional("liliguang")]
[Optional(6), Optional(6), Optional(3), Optional(4), nil, nil, Optional(9)]
["aaaa", "bbbb", "cc", "ddd", "eeeee"]
[4, 4, 2, 3, 5]
------
[4, 4, 2, 3, 5]
compactMap : 以前是flatMap
- flatMap返回后的数组中不存在nil,同时它会把Optional解包
override func viewDidLoad() {
super.viewDidLoad()
//可以对数组中的每一个元素做一次处理
let cast = ["Vivien", "Marlon", "Kim", "Karl",nil, nil, "liliguang"]
let lowercaseNames = cast.compactMap { $0?.lowercased() } //理解 : 调用$0的对象方法
print(lowercaseNames)
let letterCounts = cast.compactMap { $0?.count }
print(letterCounts)
print(stringArray.compactMap( lowStr ) )
print(stringArray.compactMap( stringCount ) )
print(stringArray.compactMap({ string -> Int in
return string.count
}))
}
func lowStr(str : String) -> String {
return str.lowercased()
}
func stringCount(string: String) -> Int {
return string.count
}
打印结果
["vivien", "marlon", "kim", "karl", "liliguang"]
[6, 6, 3, 4, 9]
["aaaa", "bbbb", "cc", "ddd", "eeeee"]
[4, 4, 2, 3, 5]
------
[4, 4, 2, 3, 5]
filer:过滤,可以对数组中的元素按照某种规则进行一次过滤
override func viewDidLoad() {
super.viewDidLoad()
//可以对数组中的每一个元素做一次处理
let cast = ["Vivien", "Marlon", "Kim", "Karl",nil, nil, "liliguang"]
let newCase = cast.filter{ string -> Bool in
return string == "liliguang"
}
print(newCase)
}
打印结果
[Optional("liliguang")]
reduce : 计算,可以对数组的元素进行计算
override func viewDidLoad() {
super.viewDidLoad()
//可以对数组中的每一个元素做一次处理
let stringArray = ["Objective-C", "Swift", "HTML", "CSS", "JavaScript"]
// reduce方法中的第一个参数是初始值
print(stringArray.reduce("", appendString))
print(stringArray.reduce("", {(string1, string2) -> String in
return string1 == "" ? string2 : string1 + "、" + string2
}) )
// $0表示计算后的结果, $1表示数组中的每一个元素 <======
print(stringArray.reduce("", {
return $0 == "" ? $1 : $0 + "、" + $1
}))
//第一次 : $0 = "" $1 = "Objective-C"
//第二次 : $0 = "Objective-C" $1 = "Swift"
//第三次 : $0 = "Objective-C、Swif" $1 = "HTML"
//......
let numbers = [1, 2, 3, 4]
let numberSum = numbers.reduce(0, { x, y in
x + y
})
print(numberSum)
}
func appendString(string1: String, string2: String) -> String {
return string1 == "" ? string2 : string1 + "、" + string2
}
打印结果
Objective-C、Swift、HTML、CSS、JavaScript
Objective-C、Swift、HTML、CSS、JavaScript
Objective-C、Swift、HTML、CSS、JavaScript
10