How do write this
switch parameter {
case .CaseA(let valueA):
print(valueA)
}
as an If condition statement? This doesn't work:
if parameter == .CaseA(let valueA) {
print(valueA)
}
You can use if case
as follows
enum Foo {
case A(Int)
case B(String)
}
let parameter = Foo.A(42)
/* if case ... */
if case .A(let valueA) = parameter {
print(valueA) // 42
}
The if case
pattern matching is equivalent to a switch
pattern matching with an empty (non-used) default case, e.g.
/* switch ... */
switch parameter {
case .A(let valueA):
print(valueA) // 42
case _: ()
}
For details, see the Language Reference - Patterns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With