Aside from all of the new features of SwiftUI in iOS 16, Apple additionally introduced Swift 5.7 which is able to come together with the discharge of Xcode 14. Let’s try one minor however welcome change in Swift 5.7.
Swift has the idea of optionals that many programming languages don’t have. An non-compulsory sort implies that it may well both have a worth or there isn’t a worth. Swift forces you to examine if an non-compulsory has a worth earlier than utilizing it.

Elective Binding is a standard solution to discover out whether or not an non-compulsory has a worth or not. Here’s a pattern code snippet utilizing non-compulsory binding:
if let myPhone = telephone
print(“Calling ” + myPhone)
var telephone: String?
if let myPhone = telephone print(“Calling “ + myPhone)
|
In the event you’re new to Swift, the if let
key phrase implies that if the non-compulsory telephone
accommodates a worth, the worth is saved to myPhone
. Contained in the if
block, myPhone
is a continuing that should comprise a worth.
To simplify the fixed or variable naming, we normally write the code like this:
if let telephone = telephone print(“Calling “ + telephone)
|
We make the fixed identify the identical because the non-compulsory.
Elective Binding in Swift 5.7
In Swift 5.7, Apple additional permits us to simplify the code like beneath:
if let telephone print(“Calling “ + telephone)
|
It is a minor change in Swift 5.7. Nonetheless, as non-compulsory binding is usually utilized in writing Swift code, this could prevent just a few keystrokes and make the code extra readable.
Observe: If you’re new to Swift, you’ll be able to try our free Swift guide to start out studying the Swift programming language.