Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary contains a certain value swift 3

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.

like image 767
Jevon Charles Avatar asked Oct 16 '16 06:10

Jevon Charles


People also ask

How do you check if a dictionary contains a value in Swift?

The contains() method returns: true - if the dictionary contains the specified key or value. false - if the dictionary doesn't contain the specified key or value.

Can a dictionary key have multiple values Swift?

In Swift, dictionaries are unordered collections that store multiple key-value pairs with each value being accessed via a corresponding unique key.

How do you check a dictionary contains a key in Swift 5?

To check if a specific key is present in a Swift dictionary, check if the corresponding value is nil or not. If myDictionary[key] != nil returns true, the key is present in this dictionary, else the key is not there.

How do you check if a key-value pair exists in a dictionary C#?

ContainsKey() Method. This method is used to check whether the Dictionary<TKey,TValue> contains the specified key or not. Syntax: public bool ContainsKey (TKey key);


3 Answers

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)
like image 130
Martin R Avatar answered Oct 21 '22 20:10

Martin R


Since your values are AnyObjectAny in Swift 3 - you have to check if the value is a string. If yes check if the value contains the substring.

let countDic : [String:Any] = ["alpha" : 1, "beta" : "foo", "gamma" : "bar"]

countDic.contains { (key, value) -> Bool in
  if let string = value as? String { return string.contains("oo") }
  return false
}

However if you want to check if any of the values is equal to (rather than contains) a string you could use also the filter function and isEmpty

!countDic.filter { (key, value) -> Bool in
  value as? String == "foo"
}.isEmpty
like image 22
vadian Avatar answered Oct 21 '22 18:10

vadian


You may need to learn basic usage of contains(where:) for Dictionarys first:

For [String: Int]:

let myIntDict1: [String: Int] = [
    "a" : 1,
    "b" : 101,
    "c" : 2
]
let myIntDict1ContainsIntGreaterThan100 = myIntDict1.contains {
    key, value in //<- `value` is inferred as `Int`
    value > 100 //<- true when value > 100, false otherwise
}
print(myIntDict1ContainsIntGreaterThan100) //->true

For [String: String]:

let myStringDict1: [String: String] = [
    "a" : "abc",
    "b" : "def",
    "c" : "ghi"
]
let myStringDict1ContainsWordIncludingLowercaseE = myStringDict1.contains {
    key, value in //<- `value` is inferred as `String`
    value.contains("e") //<- true when value contains "e", false otherwise
}
print(myStringDict1ContainsWordIncludingLowercaseE) //->true

So, with [String: AnyObject]:

let myAnyObjectDict1: [String: AnyObject] = [
    "a" : "abc" as NSString,
    "b" : 101 as NSNumber,
    "c" : "ghi" as NSString
]
let myAnyObjectDict1ContainsWordIncludingLowercaseE = myAnyObjectDict1.contains {
    key, value in //<- `value` is inferred as `AnyObject`
    //`AnyObject` may not have the `contains(_:)` method, so you need to check with `if-let-as?`
    if let stringValue = value as? String {
        return value.contains("e") //<- true when value is a String and contains "e"
    } else {
        return false //<- false otherwise
    }
}
print(myAnyObjectDict1ContainsWordIncludingLowercaseE) //->false

So, in your case:

let countDic: [String: AnyObject] = [
    "a" : 1 as NSNumber,
    "b" : "no" as NSString,
    "c" : 2 as NSNumber
]
let countDicContainsString = countDic.contains {
    key, value in //<- `value` is inferred as `AnyObject`
    value is String //<- true when value is a String, false otherwise
}
print(countDicContainsString) //->true
like image 32
OOPer Avatar answered Oct 21 '22 19:10

OOPer