let a: Int? = 5 let b: Int? = nil let result = a + b print(result ?? "nil")
Swift does not allow direct use of '+' operator between optionals without unwrapping. This code will not compile because '+' is not defined for 'Int?' and 'Int?'.
Swift requires explicit handling of optionals to avoid unexpected nil values causing runtime crashes. Operator safety forces developers to unwrap optionals safely.
let x: Int? = 10 let y: Int = 5 let sum = x + y print(sum)
The '+' operator is not defined between 'Int?' and 'Int' types directly. You must unwrap the optional before adding.
let a: Int? = 3 let b: Int? = 7 if let x = a, let y = b { print(x + y) } else { print("nil value") }
Both optionals are safely unwrapped using 'if let', so the sum 3 + 7 = 10 is printed.
Operator safety requires explicit handling of optionals, which helps prevent runtime crashes caused by nil values, making Swift code more reliable.
