Tsallaka zuwa abun ciki
Justice Tech School

Swift Guard

guard checks a condition and exits early if it fails, keeping the main path flat. It is often used to unwrap optionals.

Example
func greet(_ name: String?) {
    guard let n = name else { print("no name"); return }
    print("Hello \(n)")
}
greet(nil)
greet("Ada")

Exercise

Exit early keyword.

 let x = y else { return }

Quiz Test

What must guard do in its else block?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.