Map
map可以遍历集合并对每个元素进行处理,得到新的Optional元素,添加到新的数组中进行返回,新元素可能为nil。
func testMap() -> () {
let stringArray = ["123", "234", "345", "456", "abc"]
var result = stringArray.map(intoInt)
print(result)
// [Optional(123), Optional(234), Optional(345), Optional(456), nil]
result = stringArray.map({string -> Int? in
return Int(string)
})
print(result)
// [Optional(123), Optional(234), Optional(345), Optional(456), nil]
// $0代表数组中的每一个元素
result = stringArray.map{
return Int($0)
}
print(result)
// [Optional(123), Optional(234), Optional(345), Optional(456), nil]
result = stringArray.map { (string) in
Int(string)
}
print(result)
// [Optional(123), Optional(234), Optional(345), Optional(456), nil]
}
func intoInt(string: String) -> Int? {
return Int(string)
}
compactMap
相对于Map而言,则会将元素解包,并筛除nil
func testCompactMap() -> () {
let stringArray = ["123", "234", "345", "456", "abc"]
var result = stringArray.compactMap(intoInt)
print(result)
// [123, 234, 345, 456]
result = stringArray.compactMap({string -> Int? in
return Int(string)
})
print(result)
// [123, 234, 345, 456]
// $0代表数组中的每一个元素
result = stringArray.compactMap{
return Int($0)
}
print(result)
// [123, 234, 345, 456]
result = stringArray.compactMap { (string) in
Int(string)
}
print(result)
// [123, 234, 345, 456]
}
compactMapValues
这个是针对于Dictionary的, 将Dictionary中的value进行解包,并将nil过滤掉
func testCompactMapValues() -> () {
let dic = ["key1":"123","key2":"abc","key3":nil,nil:"123",]
var result = dic.compactMap { $0 }
print(result)
// [(key: Optional("key3"), value: nil), (key: Optional("key1"), value: Optional("123")), (key: Optional("key2"), value: Optional("abc")), (key: nil, value: Optional("123"))]
let result1 = dic.compactMapValues { $0 }
print(result1)
// [Optional("key2"): "abc", Optional("key1"): "123", nil: "123"]
}
flatmap
有两种重载:
第一种:
extension SequenceType {
public func flatMap<S : SequenceType>(transform: (Self.Generator.Element) -> S)
-> [S.Generator.Element]
}
闭包参数是(Self.Generator.Element) -> S, S 定义成SequenceType。所以它是接受数组元素,然后输出一个 SequenceType 类型的元素的闭包。但其实,flatMap 的最终返回结果却是 SequenceType 内部元素组成的新的数组,即:[S.Generator.Element]。比如示例中的arr3.flatMap({$0})
第二种:
extension SequenceType {
public func flatMap<T>(transform: (Self.Generator.Element) -> T?)
-> [T]
}
闭包参数是(Self.Generator.Element) -> T?, T 是一个 Optional 的任意类型。而 flatMap 最终返回结果,其实也不是 T? 类型,而是 T? 类型解包之后,不为 .None 的数组:[T]。比如示例中的arr1.flatMap({Int($0)})
所以基本上就是
- 将结果nil过滤掉
public func flatMap(_ transform: (Element) throws -> String?) rethrows -> [String]
注意:4.1之后,若返回的结果是Option,会弹出警告,提示用compactMap代替flatMap了。 - 若是二维数组,会将二维数组降为一维数组
- 会将 Option 进行解包
let arr1 = ["123", "234", "abc"]
let arr2 = [["123", "234", "345"], ["456", "567", "abc"]]
let arr3 = [["123", "234", "345"], ["456", "567", "678"]]
let arr4 = [["123", "234", "345"], ["456", "567", nil]]
let arr5 = [["123", "234", "345"], ["456", "567", "678"], nil]
print(arr1.flatMap({Int($0)}))
//[123, 234]
print(arr2.map({$0.map({Int($0)})}))
//[[Optional(123), Optional(234), Optional(345)], [Optional(456), Optional(567), nil]]
print(arr2.flatMap({$0.flatMap({Int($0)})}))
//[123, 234, 345, 456, 567]
print(arr3.flatMap({$0}))
//["123", "234", "345", "456", "567", "678"]
print(arr4.flatMap({$0}))
//[Optional("123"), Optional("234"), Optional("345"), Optional("456"), Optional("567"), nil]
print(arr5.flatMap({$0}))
//[["123", "234", "345"], ["456", "567", "678"]]
filter
是将元素进行规则比较,返回Bool值,为true的则返回,返回false的则被筛选掉
let dic = ["key1":"123","key2":"abc","key3":nil,nil:"123",]
let arr1 = ["123", "234", "abc"]
print(dic.filter({$0.value != nil}))
//[Optional("key2"): Optional("abc"), nil: Optional("123"), Optional("key1"): Optional("123")]
print(arr1.filter({Int($0) != nil}))
//["123", "234"]
reduce
将元素与一个初始值进行计算操作返回结果
let arr1 = ["123", "234", "abc"]
print(arr1.reduce("012",{print($0);print($1);return $0+$1}))
// 012
// 123
// 012123
// 234
// 012123234
// abc
// 012123234abc
网友评论