Aleksandar Tasevski
Aleksandar Tasevski

Reputation: 3

Get value from multidimensional array in swift

I have the following array in Swift:

var words = [
        "English" : ["Hello", "Bye"],
        "Spanish" : ["Hola", "Adios"]
    ]

How can I get the value for index, something as the following doesn't work

print(words["English"][0])

It throws the error: Value of optional type Array? not unwrapped, did you mean to use ! or ? but that just makes it:

print(words["English"]?[0])

and still doesn't work, please help.

Upvotes: 0

Views: 852

Answers (1)

Wyetro
Wyetro

Reputation: 8588

You need to look into how to unwrap optionals. For example, what you are trying to do could be done either of these two ways:

Force unwrapping:

print(words["English"]![0])

Safe unwrapping:

if let hello = words["English"]?[0]{
    print(hello)
}

Upvotes: 1

Related Questions