-2

Creted an Array like this:

var MyArray: [String:[String:[Int]]] = [
"xx": ["x1": [1, 2, 3], "x2": [4, 5, 6], "x3": [7, 8, 9]],
"yy": ["y1": [10, 11, 12], "y2": [13, 14, 15], "y3": [16, 17, 18]]

]

I want to check if for example "xx" is in the MyArray. Tried this but it has an error:

if [MyArray].contains("xx"{

                    }

But it autocorrects it to this

if [MyArray].contains(where: "xx"{

                    }

And again it has an error, any ideas??

Alex Karapanos
  • 895
  • 2
  • 7
  • 20
  • 7
    As was pointed out [in your previous question](http://stackoverflow.com/questions/41103511/how-to-save-a-multidimensional-array-to-userdefaults), `MyArray` isn't an array, it's a dictionary. – rmaddy Dec 12 '16 at 18:46
  • 1
    The error you're getting (btw, it *might* be useful to tell us what the error says) is because your syntax is completely botched. Why are you wrapping your "array" (it's a dict) into a new array? Where's the closing parenthesis for the `contains` call? – EmilioPelaez Dec 12 '16 at 19:04

4 Answers4

1

I recommend for you to use optional binding:

var MyArray: [String:[String:[Int]]] = [
    "xx": ["x1": [1, 2, 3], "x2": [4, 5, 6], "x3": [7, 8, 9]],
    "yy": ["y1": [10, 11, 12], "y2": [13, 14, 15], "y3": [16, 17, 18]]

]

if let outerDict = MyArray["xx"] {

    for entry in outerDict {
        print(entry)
    }

}

Output:

("x1", [1, 2, 3])
("x3", [7, 8, 9])
("x2", [4, 5, 6])
Alex S
  • 572
  • 7
  • 15
  • 1
    That's not optional chaining. That's optional unwrapping. – rmaddy Dec 12 '16 at 18:58
  • @AlexS about the naming, you can see [here](https://stackoverflow.com/questions/25828301/how-is-swift-if-let-evaluated/38041086#38041086) – mfaani Dec 12 '16 at 19:01
1

Since your "array" is a dictionary, and you ascertain whether one of its keys (key "xx") exists or not, a very straightforward way is simply attempting to access that that particular key of the dictionary.

var myDictionary: [String:[String:[Int]]] = [
    "xx": ["x1": [1, 2, 3], "x2": [4, 5, 6], "x3": [7, 8, 9]],
    "yy": ["y1": [10, 11, 12], "y2": [13, 14, 15], "y3": [16, 17, 18]]
]

if let _ = myDictionary["xx"] {
    print("Dictionary contains a key 'xx'")
}

W.r.t. your attempted use of contains applied to the dictionary: another approach is explicitly checking if the collection of key's to the dictionary contains your particular key:

if myDictionary.keys.contains("xx") {
    print("Dictionary contains a key 'xx'")
}

Or, directly using only the key in the (key, element) tuple of each dictionary item to check whether a given key exist in the dictionary:

if myDictionary.contains(where: { $0.key == "xx" }) {
    print("Dictionary contains a key 'xx'")
}
dfrib
  • 70,367
  • 12
  • 127
  • 192
0

First, MyArray is a dictionary not an array, actually it is a dictionary which key is string and the value is also a dictionary which its key is a string and its value is array of integers.

So, if you want to check if the dictionary contains some key or not, you can do that in the if condition like that:

if let value = MyArray["xx"] {
    // do some stuff    
}
Mohammed Elrashidy
  • 1,820
  • 1
  • 14
  • 16
0

The Swiftiest way if you don't need the value:

if let _ = MyArray["xx"]{
    print("something or do something")
}

If you need the value do:

guard let someValue = MyArray["xx"]{
   return} else{
   someFunc(someValue)
    someProperty = someValue
}
mfaani
  • 33,269
  • 19
  • 164
  • 293