gpt4 book ai didi

arrays - 无法使用 firstIndex :where in Swift 4

转载 作者:搜寻专家 更新时间:2023-11-01 06:54:01 24 4
gpt4 key购买 nike

当我在字典数组上使用 firstIndex 时,出现 Incorrect argument label in call (have 'where:', expected 'of:') 错误。

let d: [NSMutableDictionary] = [["u": 1], ["u": 2], ["u": 3]]

let i = d.firstIndex(where: { dict -> Bool in
return dict["u"] == 2
})

incorrect arg label

为什么会发生这种情况以及如何解决?

最佳答案

Swift 无法知道 == 2 是否可以与 dict["u"] 一起使用。 知道 dict["u"] 是一个 Int,但是 Swift 不知道,因为通过将这些字典键入 NSMutableDictionary 你隐藏了值的类型。

要解决这个问题,请将 [NSMutableDictionary] 更改为 [[String:Int]]

关于arrays - 无法使用 firstIndex :where in Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54995883/

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