optional binding

// ⭐️ if let/var
if let x = x { ... }
if var x = x { ... }    // `x` is mutable
if let x { ... }        // ⭐️ Swift 5.7: `= x` omitted

// ⭐️ guard let
guard let x = x else { ... }    // early exit

// ⭐️ while let
while let x = x { ... }

// ⭐️ multiple optional bindings & boolean conditions
if 
    let a = Int("4"),     // optional binding
    let b = Int("42"),    // optional binding
    a < b && b < 100      // boolean condition
{ ... }

// while let
while 
    let cat = cats[i], 
    cat.vaccinated 
{ ... }

Last updated

Was this helpful?