在Swift中展平数组数组

前端之家收集整理的这篇文章主要介绍了在Swift中展平数组数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在Swift中有一个对应的平台在Scala,Xtend,Groovy,Ruby和co?
var aofa = [[1,2,3],[4],[5,6,7,8,9]]
aofa.flatten() // shall deliver [1,3,4,5,9]

当然我可以使用减少为那,但是那种吸

var flattened = aofa.reduce(Int[]()){
    a,i in var b : Int[] = a
    b.extend(i)
    return b
}
Swift 3.0

减少:

let numbers = [[1,9]]
let reduced = numbers.reduce([],+)

flatMap:

let numbers = [[1,9]]
let flattened = numbers.flatMap { $0 }

加入:

let numbers = [[1,9]]
let joined = Array(numbers.joined())
原文链接:https://www.f2er.com/swift/321080.html

猜你在找的Swift相关文章