Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Object-Oriented Swift Properties Optional Property

dexter foo
dexter foo
8,233 Points

Not really following how the if let statement works in this case

let toy = Electronic(title: "RC Car", price: 79) 
toy.batteries = true
if let batteries = toy.batteries { 
     if batteries{
          println("Batteries included.")
     } else {
          println("Batteries not included.")
     }
}

Based on the above code, the if let statement will print "Batteries included". But however if the toy.batteries = false, it will print "Batteries not included"

I dont understand why the condition, if batteries{ println("Batteries included") only prints if the condition toy.batteries = true is met.

Shouldn't it be if batteries = true { println("Batteries included"), then it will print "Batteries included" when toy.batteries = true?

Help im getting a bit confused here since there isn't any conditions stated for the if let statement!

3 Answers

Frank Novello
Frank Novello
19,299 Points

can we use this instead i think it reads better

if(toy.batteries != nil) {

}

Lukas Smith
Lukas Smith
4,026 Points

ooo it is easy always every language (i think) you can use if VALUE it is the same if you use IF VALUE = TRUE the same You can use IF !VALUE it is the same IF VALE = FALSE (if value exist is like default) in PHP for example u can use

(value) ? true : false

the same like

if (value = true) { sth for true; } else { smh for false; }

dexter foo
dexter foo
8,233 Points

um i'm sorry but i didnt get anything you were trying to explain.

what i'm trying to ask is that how does the if let statement condition knows that it will only print "Batteries included" when toy.batteries = true when the condition only states if batteries.

Vaclav Mlejnsky
Vaclav Mlejnsky
6,832 Points

if batteries {} is same as asking if batteries != nil {}

dexter foo
dexter foo
8,233 Points

oh thanks!! does this apply to all if statements? whereby if a condition is not stated as in the example, it is the same as != nil ?

Vaclav Mlejnsky
Vaclav Mlejnsky
6,832 Points

Yes, it should be as you are saying. You can also use

if !batteries {}

instead of typing:

if batteries == nil {}

Basically you are asking if batteries contains reference to some object.

In some language (like Python) it's true. But I think it's not the case here. "if batteries" is only evaluating if the variable batteries is true or false.