Learn
if let
statements are a great way to safely handle optional values. Sometimes, we might have a lot of optionals we need to unwrap! This can get very indented:
var a: Int? = 1 var b: Int? = 2 var c: Int? = 3 var shouldPrintSum = true if let a = a { if let b = b { if let c = c { if shouldPrintSum { print(a + b + c) } } } }
Developers sometimes call this nested structure a Pyramid of Doom because it looks kind of like a sideways pyramid. To avoid these, Swift allows us to bind multiple variables with a single if let
statement.
if let a = a, let b = b, let c = c, shouldPrintSum { print(a + b + c) }
Much easier to read! If all of the optionals are not nil
and all of the bools are true, then the body is entered. Otherwise, that block is skipped. If the if let
line becomes too long, you can break it up into multiple lines:
if let a = a, let b = b, let c = c, shouldPrintSum { print(a + b + c) }
Let’s implement a similar optimization to the code here.
Instructions
1.
Refactor the code to use a single if let
statement.
Take this course for free
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.