gpt4 book ai didi

Swift 过滤器和字典

转载 作者:行者123 更新时间:2023-11-30 12:21:55 26 4
gpt4 key购买 nike

我正在尝试使用闭包过滤器来过滤字典中 18 岁以下的人。我知道如何使用数组进行过滤,但我不确定如何过滤字典。

var namesAndAges = ["Tom": 25, "Michael": 35, "Harry": 28, "Fabien": 16]
var underAge = namesAndAges.filter { &0.namesAndAges.value < 18 }

这给了我错误“闭包参数列表的上下文类型需要 2 个参数,不能隐式忽略”

最佳答案

就这样做:

var namesAndAges = ["Tom": 25, "Michael": 35, "Harry": 28, "Fabien": 16]
var underAge = namesAndAges.filter({ $0.value < 18 }) // [(key: "Fabien", value: 16)]

您所做的就是在过滤器中使用$0.value

关于Swift 过滤器和字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44703425/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com