从数组Array返回Array的函数

时间:2015-07-10 18:37:48

标签: ios swift

我试图创建一个函数,它接受一个返回单个数组的数组,以便所有值("名称和#34;)都有自己的索引。

var namesList1 = [String]()
var namesList2 = [String]()
var namesList3 = [String]()

namesList1 = ["Paul","John","Ringo","George"]
namesList2 = ["Julie","Sarah","Jackie"]
namesList3 = ["Jim","Jack","Charlie","Sally","Debra"]

var namesCombinedArray = [NSArray]()
namesCombinedArray = [namesList1,namesList2,namesList3]





func total(arrays:NSArray) -> NSArray{

// how to loop to create an array with index(s) to all names ??
 var completeList = [NSArray]()

return completeList

}

1 个答案:

答案 0 :(得分:4)

您可以使用flatMap

let namesList1 = ["Paul","John","Ringo","George"]
let namesList2 = ["Julie","Sarah","Jackie"]
let namesList3 = ["Jim","Jack","Charlie","Sally","Debra"]
let namesCombinedArray = [namesList1,namesList2,namesList3]

let completeList = namesCombinedArray.flatMap{$0}

print(completeList)
// [Paul, John, Ringo, George, Julie, Sarah, Jackie, Jim, Jack, Charlie, Sally, Debra]