我正在尝试过滤字典数组。以下代码是我正在寻找的场景的示例

let names = [
    [ "firstName":"Chris","middleName":"Alex"],
    ["firstName":"Matt","middleName":""],
    ["firstName":"John","middleName":"Luke"],
    ["firstName":"Mary","middleName":"John"],
]

最终结果应该是一个具有中间名的数组。

最佳答案

这成功了

names.filter {
  if let middleName = $0["middleName"] {
    return !middleName.isEmpty
  }
  return false
}

09-25 16:07