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'?

3      

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.

4      

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.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.