UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Optionals Summary Quiz: Why doesn't this function need "guard let"?

Forums > 100 Days of Swift

In this code, we see that 'guard' does not need 'guard let'

class Camel {
    var humps: Int
    init?(humpCount: Int) {
        guard humpCount <= 2 else { return nil }
        humps = humpCount
let horse = Camel(humpCount: 0)

However, in the optionals quiz, we are told that it is necessary that this code requires 'guard let'

func brewBeer(to strength: Double?) {
    guard strength = strength else { return } //invalid! We need 'guard let'
    print("Let's brew some beer!")
brewBeer(to: 5.5)

Why? Is it because in the first piece of code, 'humpCount' is not being assigned a value? So it doesn't need a 'let'?


Those two guard examples are doing different things.

In the first example, humpCount is not an Optional. The guard statement is just checking that it has a value less than or equal to 2. So just plain guard is fine.

In the second example, strength is an Optional and the guard statement is checking that it is not nil and, if it isn't, assigning the unwrapped value to a locally-scoped constant also called strength. For that, you do need guard let to assign the unwrapped value.


You shall not pass!

When I first read about Swift's guard statement, I thought it was brilliant. I imagined Beefeater guards standing outside the Towers of London in their impratical scarlet and gold uniforms with their pikes challenging anyone trying to get in.

If you knew the password, or had the right credentials, the Beefeaters let you pass. You were free to enter and go where you pleased. If not, you weren't even allowed inside. End of story.

As @rooster points out in the camel example, the guard on duty is only checking to see that the humpCount is less than or equal to 2. If so, the guard lets the variable into the function to have more fun. Have a nice day, sir. If the guard sees you're trying to get in with more than 2 humps, he denies your visa. No entry for you!

In the second example, the guard challenges the variable strength. Before being allowed to pass the guard, the variable must prove that it actually has a value and isn't nil. If the variable is nil, access denied. There's the exit. Scram.


Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.