solution1 ( ):
allItems += allPeople.flatMap{$0.items}
let newArray = allItems + allPeople.flatMap{$0.items}
// Using 'map' won't work:
allItems = allItems + allPeople.map{$0.items} //error: binary operator '+' cannot be applied to operands of type '[String]' and '[[String]]'
, [String]] [String], + , , :
let john = Person()
john.items = ["a", "b", "c"]
let jane = Person()
jane.items = ["d", "e", "f"]
allPeople.append(john)
allPeople.append(jane)
print(allPeople.map{$0.items}) // [["a", "b", "c"], ["d", "e", "f"]] <-- [[String]]
// and obviously can't be added to [String]
. .
let mapped = allPeople.map{$0.items}
print(Array(mapped.joined())) // ["a", "b", "c", "d", "e", "f"]
, map, joined
solution2 ( , , a flatMap - join + map)
let newJoinedMappedArray = allItems + Array(allPeople.map($0.items).joined())