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

SOLVED: day 12 - optionals summary-test 11,q6. guard //without the let

Forums > 100 Days of Swift

struct Dog {
    var name: String
    init?(name: String) {
        guard name == "Lassie" else { //error message when "guard let": Pattern matching in a condition requires the 'case' keyword
            print("Sorry, wrong dog!")
            return nil
        }
        self.name = name
    }
}
let dog = Dog(name: "Fido")

Hi, how come the guard works without the "let" keyword and does not work with "guard let"? i thought its always "guard let" but seems not true for some reason..

3      

guard doesn't always have to be followed by a let, just as if doesn't have to. It's only guard let or if let if you need to unwrap an Optional and bind it to a constant or variable or if you are pattern matching with guard case or if case.

guard simply says "check this condition and fail if it doesn't pass".

guard let is "check that this Optional has a value and fail if it is nil. Oh, and by the way, if it passes you can use the let variable in your current scope. You're welcome."

In the example provided, you are just checking the value of the name variable and failing if it doesn't match what you expect. You would use guard let here if, say, name was a String? (i.e., an optional string) and you were checking if it was nil.

4      

Ok, thanks @roosterboy. guard let is to check if optional value exists or is nil and guard is just to check if condition is true or false.

I modified code to make initialiser with optional parameter to use guard let first and then when its true, jump to guard statement to check condition.

struct DogOptional {
    var name: String?
    init?(nameOptional: String?) {
        guard let unwrapped = nameOptional else { //guard with let because parameter is optional.
            print("Sorry, entry not received!")
            return nil
        }
        guard unwrapped != "Lassie" else{
            print("Sorry, wrong dog name!")
            return nil
        }
        self.name = unwrapped
        print("Dog name is: \(name!)")
    }
}
let dogObj = DogOptional(nameOptional: "Lassie")

3      

And just as an FYI, you can often combine these two into onne guard:

        guard let unwrapped = nameOptional,  unwrapped != "Lassie" else {
            return nil
        }

Not in your specific example, if you want to maintain two separate error messages, but just be aware that it's possible.

4      

Thanks @roosterboy for showing how to shorten the code by putting the 2 conditions into one line :D

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.