if let unwrappedValue = optionalValue {
// Do something with the unwrappedValue
} else {
// Handle the case where optionalValue is nil
}
let optionalName: String? = "Alice"
if let name = optionalName {
print("Hello, \(name)!")
} else {
print("Hello, anonymous person!")
}
xxxxxxxxxx
let optionalName: String? = "John"
if let name = optionalName {
print("Hello, \(name)!")
} else {
print("Hello, anonymous!")
}
// the if let is the optional value part. If optionalName variable has a value then it will run. If the value is nil then the else statement will run.
xxxxxxxxxx
Optional binding is a way to safely check if an optional value has
a value, and if so, to unwrap it into a new constant or variable.
If the optional value is nil,
the code inside the if let or guard let block won't be executed.