if let shorthand for unwrapping optionals
About 2 min
if let shorthand for unwrapping optionals 관련
HACKING WITH SWIFT
What's new in Swift?
if let shorthand for unwrapping optionals | Changes in Swift 5.7
if let shorthand for unwrapping optionals
Available from Swift 5.7
SE-0345 apple/swift-evolution
introduces new shorthand syntax for unwrapping optionals into shadowed variables of the same name using if let
and guard let
. This means we can now write code like this:
var name: String? = "Linda"
if let name {
print("Hello, \(name)!")
}
Whereas previously we would have written code more like this:
if let name = name {
print("Hello, \(name)!")
}
if let unwrappedName = name {
print("Hello, \(unwrappedName)!")
}
This change doesn’t extend to properties inside objects, which means code like this will not work:
struct User {
var name: String
}
let user: User? = User(name: "Linda")
if let user.name {
print("Welcome, \(user.name)!")
}
Other Changes in Swift 5.7
Multi-statement closure type inference | Changes in Swift 5.7
Multi-statement closure type inference
Clock, Instant, and Duration | Changes in Swift 5.7
Clock, Instant, and Duration
Regular expressions | Changes in Swift 5.7
Regular expressions
Type inference from default expressions | Changes in Swift 5.7
Type inference from default expressions
Concurrency in top-level code | Changes in Swift 5.7
Concurrency in top-level code
Opaque parameter declarations | Changes in Swift 5.7
Opaque parameter declarations
Structural opaque result types | Changes in Swift 5.7
Structural opaque result types
Unlock existentials for all protocols | Changes in Swift 5.7
Unlock existentials for all protocols
Lightweight same-type requirements for primary associated types | Changes in Swift 5.7
Lightweight same-type requirements for primary associated types
Constrained existential types | Changes in Swift 5.7
Constrained existential types
Distributed actor isolation | Changes in Swift 5.7
Distributed actor isolation
buildPartialBlock for result builders | Changes in Swift 5.7
buildPartialBlock for result builders
Implicitly opened existentials | Changes in Swift 5.7
Implicitly opened existentials
Unavailable from async attribute | Changes in Swift 5.7
Unavailable from async attribute