John Doe
John Doe

Reputation: 2491

Not able to use firstIndex:where in Swift 4

I am getting Incorrect argument label in call (have 'where:', expected 'of:') error when I use firstIndex on an array of dictionary.

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

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

incorrect arg label

Why is this occurring and how to fix this?

Upvotes: 1

Views: 363

Answers (1)

matt
matt

Reputation: 535304

Swift has no way of knowing whether == 2 can work with dict["u"]. You know that dict["u"] is an Int, but Swift doesn't know that, because by typing these dictionaries as NSMutableDictionary you have hidden the types of the values.

To fix that, change [NSMutableDictionary] to [[String:Int]].

Upvotes: 2

Related Questions