理解compactMap和flatMap的问题

问题描述 投票:2回答:3

我已经从多个教程中了解到flatMap/compactMap用于flatten数组的数组但在我的情况下它不起作用或者我没有正确理解它。

let myArray = [["Raja","Kumar", nil,"Waqas"],["UAE","SINGAPORE","dUBAI","HONGKONG"]]
let final = myArray.compactMap{ $0 }

print("Result:\(final)")

输出:

Result:[[Optional("Raja"), Optional("Kumar"), nil, Optional("Waqas")], [Optional("UAE"), Optional("SINGAPORE"), Optional("dUBAI"), Optional("HONGKONG")]]

我尝试从上面的数组中删除nil,但它仍然没有展平我的数组。

任何帮助将非常感激。

arrays swift flatmap
3个回答
© www.soinside.com 2019 - 2024. All rights reserved.