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
Taylor Smith
iOS Development Techdegree Graduate 14,153 PointsOptionals and "!" or "?" in Swift
I'm having a hard time understanding when to use ! or ? and why. I know it says it in the videos and docs, but can anyone explain it in an easier way to grasp? it's always confused me! Thanks!
3 Answers
Ryan Ackermann
14,665 PointsI believe I have an understanding of the whole optionals concept:
Basically they are a very same coding mechanism that will allow you to write safer code, for example lets say that you have an app where you want to get the user's age, now let's say you also forgot to implement a keypad only input method so they give you Twenty for their age instead of 20. Now this would prove a problem if you were trying to test against a greater than if statement since you probably did not think of this possible error.
For example:
var input = "Twenty"
let age = input.toInt()
if age > 18 {
println("User is old enough")
} else {
println("Too young!")
}
Your would end up being too young because of bad coding. Now if you use Optionals you can safley
test for all possible scenarios! Even if you only make the user able to enter numbers, who says they don't
have some funky 3rd party keyboard that lets them input emoji into a numeric only textfield!
Updated example with Optionals:
var input = "Twenty"
let age = input.toInt()
if let userAge = age {
if userAge > 18 {
println("User is old enough")
} else {
println("Too young!")
}
} else {
println("Whoops, age not a number! try again!")
}
No this pice of logic is fool proof! This feature of Swift is called optional binding and it's one of the most sensible uses that I have found for Optionals, but there are many more.
The Optional Logic here is the:
if let userAge = age {
Line because if the constant of userAge is able to be set to age, meaning age is not nil, then the if statement succeeds and you can perform logic on the constant of userAge.
I hope this helps, I know that examples always help me! Good luck :)
Taylor Smith
iOS Development Techdegree Graduate 14,153 Pointsthank you! that really helps a lot! One more question, how do you know when to put ! and ?'s?
Ryan Ackermann
14,665 PointsYour very welcome! :)
Using ? will allow for that variable to return nil, this provides better failure scenarios.
For example:
var colorForAcceptButton: UIColor?
And ! will implicitly unwrap a type that you define, typical usage would be for instance variables that you will take care of initializing later before using them in your code.
Example:
var location: CLLocation!
Taylor Smith
iOS Development Techdegree Graduate 14,153 Pointsoh ok rad. thank you!