Check if the dictionary contains values ​​in SWIFT

is just a simple task. I have a dictionary var types = [Int: String](), it is like an empty one, after some user operations, it is filled with data. According to this dictionary I enable/disable the buttons in the UI.
Checking for emptiness is easy, but how to check whether the dictionary contains certain values?
The compiler suggested that I use predicate placeholders:

types.contains(predicate: ((Int, String)) throws -> Bool>)< /pre> 
Since you only want to check the existence of a given value, you can apply contains to the value attribute of the dictionary Method (given a native Swift dictionary), for example:

var types: [Int: String] = [1: "foo", 2: "bar" ]
print(types.values.contains("foo")) // true

As mentioned in @njuri: answer, using the values ​​attribute of the dictionary seems to incur overhead (my I haven't verified it myself) wrt just checks the inclusion predicate directly against the value entry in the key-value tuple of each Dictionary element. Since Swift is fast, this shouldn't be a problem unless you are using a huge dictionary. Anyway. , If you want to avoid using the values ​​attribute, you can check the alternative method given in the previously mentioned answer, or use another alternative method (dictionary extension), as shown below:

extension Dictionary where Value: Equatable {
func containsValue(value: Value) -> Bool {
return self.contains {$0.1 == value }
}
}< br />
types.containsValue("foo") // true
types.containsValue("baz") // false

Just simple Task. I have a dictionary var types = [Int: String](), it is like an empty one, after some user operations, it is filled with data. According to the blanks in this dictionary or some specific data, I enable/disable Buttons in the UI.
Checking for emptiness is easy, but how to check whether the dictionary contains certain values?
The compiler suggested that I use predicate placeholders:

types.contains(predicate: ((Int, String)) throws -> Bool>)< /pre>

Since you only want to check the existence of a given value, you can apply the contains method to the value attribute of the dictionary (given a native Swift dictionary), for example:

var types: [Int: String] = [1: "foo", 2: "bar"]
print(types.values.contains ("foo")) // true

As mentioned in @njuri: answer, using the values ​​attribute of the dictionary seems to incur overhead (I haven’t verified it myself) wrt is just for each Dictionary directly The value entry check in the key-value tuple of the element contains the predicate. Since Swift is fast, this shouldn't be a problem unless you are using a huge dictionary. Anyway, if you want to avoid the values ​​attribute, you can check The alternative method given in the previously mentioned answer, or use another alternative method (dictionary extension), as follows:

extension Dictionary where Value: Equatable {
func containsValue(value: Value) -> Bool {
return self.contains {$0.1 == value }
}
}

types.containsValue(" foo") // true
types.containsValue("baz") // false

Leave a Comment

Your email address will not be published.