Cloud function to fetch rawda prayer schedule
- Faiz Umar Baraja

- May 14, 2024
- 1 min read
Using optional binding is very common in Swift. With the "if let" shorthand syntax it is easier to write and read. It has been introduced with Swift 5.7 and Xcode 14.
if let for optional binding
Optional binding is used to access optional in a safe way. For example, take a look at the following example:
var a: Int? = 5
if let a = a {
print(a)
}It’s not only more clear, but it’s also less error-prone: It’s common practice to use the same variable for the shadowing variable. However, if the name of the variable is long and difficult, a typo can easily happen. Then, you have two similar named variables accessible inside the code block. With the if let shorthand syntax, this cannot happen.
guard
Not only if let is often used for handling optional, but guard as well. Here you have a similar shorthand syntax:
func doSomething(a:Int?) {
guard let a else {
return
}
print(a)
}


Comments