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
Ryan Lail
4,386 PointsI need help understanding the 'if let' statement and 'unwrapping'.
Hey,
Can someone explain to me what unwrapping is and how the if let statement works?
Thanks! ~Ryan!
1 Answer
Nick Kohrn
38,025 PointsHi Ryan,
Let's say that you have a couple of variables:
var a = 1
var b: Int?
In the variables above, you know that a has a value of 1, but let's pretend that you are not sure what the value of b is until after your user has done something in an application, say something like entering the number of siblings they have on a survey. We know it will be an integer, but we don't know what the actual value will be so we set it as an Optional.
When a variable is created without an initial value, say, when the survey is initially loaded with no default values, it is of type Optional. As you can see, a has a value of 1, while b has no assigned value, which defaults to nil. You can see this by printing the value of b:
println(b) // prints nil to the console
Now, using the variables above, I am using if let to safely get the value of b and assign it to c:
if let c = b {
println("c has a value of \(c).")
} else {
println("c is nil.")
}
This code will print "c is nil." because when we try to assign the value of b to a new constant named c on the first line, c is nil because b is nil. The way the line reads is this:
'If b has a value, assign it to a constant named c.'
Since b is nil, c cannot be assigned an integer value, and so c must also be nil, which executes the else statement.
Now, we will assign b an integer value:
var a = 1
var b: Int?
b = 2
if let c = b {
println("c has a value of \(c).")
} else {
println("c is nil.")
}
Since b now has a value of 2, when we try to assign c the value of b, which is now 2, c can successfully be created with a value of 2. "c has a value of 2." will be printed because c was successfully created as a constant with the valid integer value of b since b is no longer nil.
Just as before, if let basically says 'If b has a value, assign it to a constant named c.'
Hope this helps!
Ryan Lail
4,386 PointsRyan Lail
4,386 PointsGreat! Thanks so much! Would you know why optionals are 'wrapped' in the first place?
Nick Kohrn
38,025 PointsNick Kohrn
38,025 PointsRyan,
Glad I could help!
Here is a link to the official Apple documentation on Optionals. It's about two-thirds down the page.
Also, here is another link that was written by Amit, one of the iOS teachers here at Treehouse.
Check out these links and they should have you pretty well on your way to understanding the reason for Optionals, wrapping, and unwrapping better than I can explain.
Hope this helps!