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

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4176 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.