Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Swift Dictionary

  1. Home
  2. Swift Dictionary
  3. guard / guard let / guard var

guard / guard let / guard var

The Swift guard statement enforces an early return when a condition is not met. It combines Optional unwrapping and condition checks in one place, reducing nesting and making your code easier to read.

Syntax

// Basic guard statement
guard condition else {
    // Runs when the condition is not met (return/throw/break required)
    return
}

// guard let — unwrap an Optional
guard let constantName = optionalValue else {
    return
}
// constantName is available as a non-Optional from here on

// Multiple conditions
guard let x = optX, let y = optY, x > 0 else {
    return
}

Syntax Overview

SyntaxDescription
guard condition else { return }Returns early if the condition is false.
guard let constant = optional else { }Unwraps an Optional and returns early if it is nil.
guard var variable = optional else { }Unwraps an Optional into a mutable variable.
guard condition1, condition2 else { }Checks multiple conditions at once, separated by commas.
guard ... else { throw error }Throws an error when the condition is not met.
guard ... else { break }Breaks out of a loop when the condition is not met.

Sample Code

// Unwrap an Optional with guard let
func greet(name: String?) {
    guard let name = name else {
        print("No name provided")
        return
    }
    // name is a non-Optional String from here on
    print("Hello, \(name)!")
}

greet(name: "Taro")
greet(name: nil)

// guard with multiple conditions
func processOrder(userID: Int?, amount: Double?) {
    guard let userID = userID, let amount = amount, amount > 0 else {
        print("Invalid order information")
        return
    }
    print("Order from user \(userID): $\(amount)")
}

processOrder(userID: 42, amount: 1500.0)
processOrder(userID: nil, amount: 500.0)
processOrder(userID: 10, amount: -100.0)

// Comparison with if let (guard produces less nesting)
func checkAge(age: Int?) {
    guard let age = age, age >= 18 else {
        print("Under 18 or age unknown")
        return
    }
    print("Age \(age) is valid")
}

checkAge(age: 25)
checkAge(age: 15)

Notes

The else clause of a guard statement must always end with a control transfer statement such as return, throw, break, or continue. This guarantees that any code after the guard only runs when all conditions have been met.

Constants and variables unwrapped by guard let are available throughout the enclosing scope — everywhere except inside the else clause itself. Unlike if let, this lets you work with unwrapped values without additional nesting. Forgetting the control transfer statement at the end of the else clause causes a compile error.

For the basics of Optionals, see Optional / ? / !.

If you find any errors or copyright issues, please .