首页 > 解决方案 > 无法在 Swift 4 中使用 firstIndex:where

问题描述

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

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

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

不正确的 arg 标签

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

标签: arraysswift

解决方案


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

要解决此问题,请更改[NSMutableDictionary][[String:Int]].


推荐阅读