NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Day 12: Optionals: "Unwrapping with Guard"

Forums > 100 Days of SwiftUI


Hello. In one of Paul's Review Quiz questions and the result is that it is "valid Swift".

I don't know what "return" is doing in this example. Is this shorthand for "return nil"?

func test(number: Int?) {
  guard let number = number else
  { return }
    print("Number is \(number)")
   test(number: 42)


Return exits the function early



"Return exits the function early". Okay, got it.

What would happen in the funcion if the integer was nil?

test(number: nil)


Exactly that! The function will be exited early in the guatd statement, meaning the rest of the function isn't executed. So in this case, the print statement will not be run!


your function returns void i.e. nothing. guard statements is there to check if its nil or not. if var provided is nil then else block of the guard statement is executed. and there you wrote return which means just return from the function without trying to do anything else.


Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try 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.