Switch
Switch
Implementation
enum Animal {
case dog
case cat
case wolf
}
let animal: Animal = .wolf
switch animal {
case ...
default:
}
No - op
switch caseVar {
case .caseOne: print("1")
case .caseTwo: ()
case .caseThree: break
}
SwiftUI ViewBuilder
break does not work with ViewBuilder
switch caseVar {
case .caseOne: print("1")
case .caseTwo: EmptyView()
}
SO | noop-for-swifts-exhaustive-switch-statements
Break and Continue
andybargh | break-and-continue
if case let
The Swift If-Case-Let syntax is a convenient shortcut to avoid a full switch statement when you only want to match a single case.
Pattern Matching
You can do switch statement on enums as well as do specific case if needed.
// event: Enum type
if case .caseOne(let msg) = event {
self?.log(error.description, level: .error)
self?.playerError = error
}
Nesting cases with if let Result & enum switch type
// urlResult: Result type & urlType: enum
if case let .success(urlType) = urlResult,
case let .parsed(urlParsed) = urlType {
}
use your loaf | swift-if-case-let
if case let is an abbreviated form of switch
gosh darn | If Case let
fucking if case let syntax