Updated for Xcode 14.2
Swift gives us an alternative to if let
called guard let
, which also unwraps optionals if they contain a value, but works slightly differently: guard
let is designed to exit the current function, loop, or condition if the check fails, so any values you unwrap using it will stay around after the check.
To demonstrate the difference, here’s a function that returns the meaning of life as an optional integer:
func getMeaningOfLife() -> Int? {
42
}
And here’s that function being used inside another function, called printMeaningOfLife()
:
func printMeaningOfLife() {
if let name = getMeaningOfLife() {
print(name)
}
}
That uses if let
, so that the result of getMeaningOfLife()
will only be printed if it returned an integer rather than nil.
If we had written that using guard
let, it would look like this:
func printMeaningOfLife() {
guard let name = getMeaningOfLife() else {
return
}
print(name)
}
Yes, that’s a little longer, but two important things have changed:
guard
requires that we exit the current scope when it’s used, which in this case means we must return from the function if it fails. This is not optional: Swift won’t compile our code without the return
.It’s common to see guard
used one or more times at the start of methods, because it’s used to verify some conditions are correct up front. This makes our code easier to read than if we tried to check a condition then run some code, then check another condition and run some different code.
So, use if let
if you just want to unwrap some optionals, but prefer guard let
if you’re specifically checking that conditions are correct before continuing.
SPONSORED Thorough mobile testing hasn’t been efficient testing. With Waldo Sessions, it can be! Test early, test often, test directly in your browser and share the replay with your team.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.