Skip to content
Justice Tech School

Optional Binding Patterns

Combine if let, guard let and multiple bindings. Short form if let x works since Swift 5.7.

Example
let a: Int? = 5, b: Int? = 7
if let a, let b { print(a + b) }
if let x = a, x > 3 { print("big") }
print([a, nil, b].compactMap { $0 })

Exercise

Short binding.

if let  { }

Quiz Test

What does compactMap do with nil?

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