1

I am looking for a way to see if a value (stored in a dictionary) is in an array. The array looks like this: var array = Array<Dictionary<String, Int>>()

I looked around here, on stackoverflow, and found this: How to check if an element is in an array. Only problem is I can't seem to use the contains method by writing arrray.contains.... What am I doing wrong?

Community
  • 1
  • 1
Tice
  • 103
  • 1
  • 10
  • possible duplicate of [How to check if an element is in an array](http://stackoverflow.com/questions/24102024/how-to-check-if-an-element-is-in-an-array) - While you say "I can't seem to use the `contains` method by writing `array.contains`", the top answer to that question has both the Swift 1.2 and Swift 2.0 solutions, and you didn't bother trying the Swift 1.2 solution. – nhgrif Jul 09 '15 at 12:08

2 Answers2

6

You can use contains within contains to check if any of the dictionaries in an array contains the value you are looking for.

For example, to search array a for the value 1:

let a: [[String: Int]] = [["a": 1, "b": 2], ["c": 3], ["d": 4]]

Swift 1.2:

if contains(a, {contains($0.values, 1)}) {
    println("it is in there")
}

Swift 2.0:

/* Thanks to @Qbyte for providing this solution */
if a.contains({ $0.values.contains(1) }) {
    print("it is in there")
}

This works because each member of array a gets evaluated with the closure {contains($0.values), 1} to see if it is true. The closure takes the dictionary it is passed, gets the array of values from it and then uses contains to see if the value is in there.

vacawama
  • 150,663
  • 30
  • 266
  • 294
  • I think it's a "feature" of the `if`. It also works to do `if (a.contains {$0.values.contains(1)} ) {`. – vacawama Jul 09 '15 at 11:49
  • Interesting. I seem to be using Swift 1.2, so I tried your solution. Didn't see any IDE errors pop up, so it should work. What about checking the keys of the dictionary, how would I do that? – Tice Jul 09 '15 at 12:02
  • @Tice By applying a little logic. This isn't how Stack Overflow works. Post one question, get one answer. Don't hold this user's answer ransom until they've written your entire app for you. This answer appears to (even by your own admission) work perfectly for you and has fully answered the question. Give it a green checkmark. And then, perhaps, do a little research. vacawama has shown you have to check for the `values`... you should be able to figure out how to check for the `keys` on your own. – nhgrif Jul 09 '15 at 12:07
  • I didn't mean to offend, nor was I holding the answer ransom. I apologize if that was misunderstood. Anyhow, I have marked this as the answer. – Tice Jul 09 '15 at 12:13
1

You can use this extension:

extension Array {
    func contains<T where T : Equatable>(obj: T) -> Bool {
        return self.filter({$0 as? T == obj}).count > 0
    }
}

Which you can test like:

var array1 = Array<Dictionary<String, Int>>()
array1 = [["zero": 0],["one": 1], ["two": 2]]

array1.contains(["zero": 0])   //true
array1.contains(["five": 5])   //false
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165