compound cases
enum Job {
case developer(exp: Int)
case designer(exp: Int)
case pm(exp: Int)
}
let job = Job.developer(exp: 5)
switch job {SS
// โญ๏ธ compound cases with enums with associated values
// 1. all cases have to include same set of value bindings (let exp)
// 2. each binding has to be of the same type (Int)
case .developer(let exp), .designer(let exp), .pm(let exp):
print("You have \(exp) year(s) of experience")
}
// another example
enum Foo {
case a(Int)
case b(Int, Int)
case c(Int, String, Int)
case d(String)
}
let test = Foo.c(1, "c", 3)
switch test {
// โญ same binding (let i), same type (Int)
case .a(let i), .b(_, let i), .c(let i, _, _): print(i) // 1
default: break
}
switch test {
// โญ order doesn't matter.
case let .b(i, j), let .c(j, _, i): print("(\(i), \(j))") // (3, 1)
default: break
}
Last updated