Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Basics (retired) Collections What is a Dictionary?

Why this does not work?

With a dictionary, the following optional works:

let todo = ["Return Calls":"Pez"]
if let todoThing = todo["Return Calls"] {
    println("Tu lista si contiene la tarea de \(todoThing)")
} else {
    println("Tu lista no contiene ese elemento")
}

Whereas when using an array, it does not work:

let todo = ["Return Calls"]
if let todoThing = todo["Return Calls"] {
    println("Tu lista si contiene la tarea de \(todoThing)")
} else {
    println("Tu lista no contiene ese elemento")
}

WHY? Please help

Added syntax highlighting to your posts. :)

1 Answer

Arman Kussainov
Arman Kussainov
4,754 Points

Because when using array you can't do like this todo["Return Calls"] . You can get value of array's elements like todo[0], todo[1] and etc.

It doesn't work either. It seems this is not the correct way to use an if statement with an array.

let todo = ["Return Calls"]
if let todoThing = todo[0] {
    println("Tu lista si contiene la tarea de \(todoThing)")
} else {
    println("Tu lista no contiene ese elemento")
}