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

He Li
PLUS
He Li
Courses Plus Student 2,077 Points

why need line 26 if statement?

since there's already a if let statement on line 25, why there's still another if statement on line 26 to check whether batteries is nil?

3 Answers

Hello He,

It's really helpful if you include a little more information - line 26 of which challenge/video, or which code you're working on?

However, with swift optionals, the if let is checking if the value is nil. A nil value means the code block following won't execute. The if statement afterwards would check if the value is true or false - false means that there was a response, but it was negative, and nil means there was no response.

Hi.

Quite simply said: This is called nesting. The second if statement will be evaluated if the first if statement evaluates to true.

Hope this helped.

He Li
PLUS
He Li
Courses Plus Student 2,077 Points

Now I understand. The first if statement determine whether it's nil, and the nested one determine whether the value is true of false. I guess I messed up with nil with false.