Matching optional enums against non-optionals
About 2 min
Matching optional enums against non-optionals 관련
HACKING WITH SWIFT
What's new in Swift?
Matching optional enums against non-optionals | Changes in Swift 5.1
Matching optional enums against non-optionals
Available from Swift 5.1
Swift has always been smart enough to handle switch/case pattern matching between optionals and non-optionals for strings and integers, but before Swift 5.1 that wasn’t extended to enums.
Well, in Swift 5.1 we can now use switch/case pattern matching to match optional enums with non-optionals, like this:
enum BuildStatus {
case starting
case inProgress
case complete
}
let status: BuildStatus? = .inProgress
switch status {
case .inProgress:
print("Build is starting…")
case .complete:
print("Build is complete!")
default:
print("Some other build status")
}
Swift is able to compare the optional enum directly with the non-optional cases, so that code will print “Build is starting…”
Other Changes in Swift 5.1
Improvements to synthesized memberwise initializers | Changes in Swift 5.1
Improvements to synthesized memberwise initializers
Implicit returns from single-expression functions | Changes in Swift 5.1
Implicit returns from single-expression functions
Universal Self | Changes in Swift 5.1
Universal Self
Opaque return types | Changes in Swift 5.1
Opaque return types
Static and class subscripts | Changes in Swift 5.1
Static and class subscripts
Warnings for ambiguous none cases | Changes in Swift 5.1
Warnings for ambiguous none cases
Ordered collection diffing | Changes in Swift 5.1
Ordered collection diffing
Creating uninitialized arrays | Changes in Swift 5.1
Creating uninitialized arrays