Checking for a value Copy code Print a message when an optional has a value or is nil let input: String? = "42" if let input { print("Found value: \(input)") } else { print("No value found") }
Early exit with guard Copy code func greet(name: String?) {Guard unwrap inside a function and bail early when missing data guard let name else { print("No name provided") return }Continue execution after guard unwrap - the happy path print("Hello, \(name)!") } greet(name: nil) greet(name: "Chris")
Providing a default Copy code Fall back to a default using the nil coalescing syntax let number: Int? = nil let fallback = number ?? 42 print("Using fallback: \(fallback)")