Dictionary contains a certain value swift 3

dictionary, swift3

Solution

`contains(where:)` checks if any element of the collection satisfies the given predicate, so in your case it would be

let b = countDic.contains { (key, value) -> Bool in
    value as? String == givenString
}

or, directly applied to the `values` view of the dictionary:

let b = countDic.values.contains { (value) -> Bool in
    value as? String == givenString
}

In both cases it is necessary to (optionally) cast the `AnyObject` to a `String` in order to compare it with the given string.

It would be slightly easier with a dictionary of type `Dictionary<String, String>` because strings are `Equatable`, and the `contains(element:)` method can be used:

let b = countDic.values.contains(givenString)

Problem

I want to check if a string exists in any of the values in my Dictionary ``` Dictionary<String, AnyObject> ``` I know arrays has .contains so I would think a dictionary does too. Xcode tells me to use the following when I start typing contains ``` countDic.contains(where: { ((key: String, value: AnyObject)) -> Bool in <#code#> }) ``` I just don't understand how to use this I know inside I need to return a Bool, but I don't understand where I put what String I'm looking for. Any help would be great.

Original source