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 trialswiftfun
4,039 PointsWhy 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
1 Answer
Arman Kussainov
4,754 PointsBecause 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.
swiftfun
4,039 PointsIt 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")
}
Ryan Duchene
Courses Plus Student 46,022 PointsRyan Duchene
Courses Plus Student 46,022 PointsAdded syntax highlighting to your posts. :)