Skip to content

Instantly share code, notes, and snippets.

@0xLeif
Created September 23, 2021 00:30
Show Gist options
  • Save 0xLeif/80edced1c92bb8f088963fe03cf5e22d to your computer and use it in GitHub Desktop.
Save 0xLeif/80edced1c92bb8f088963fe03cf5e22d to your computer and use it in GitHub Desktop.
case let vs case .(let)
enum Something {
case a(value: String)
case b(value: String, times: Int)
case c(value: String, times: Int, isOn: Bool)
}
let a: Something = .a(value: "A Value")
let b: Something = .b(value: "B Value", times: 27)
let c: Something = .c(value: "C Value", times: 27, isOn: false)
let something: Something = b
if case .a = something {
print("Ignore the value")
} else if case .b(let value, let times) = something {
print(String(repeating: value, count: times))
} else if case let .c(value, times, isOn) = something {
if isOn {
print(String(repeating: value, count: times))
}
}
switch something {
case .a:
print("Ignore the value")
case .b(let value, let times) :
print(String(repeating: value, count: times))
case let .c(value, times, isOn):
if isOn {
print(String(repeating: value, count: times))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment