Swift "h" must be bound in every pattern error - Switch problems
You may need to write something like this:
switch hour {
case let h? where h >= 17:
return true
default:
return false
}
Or, as suggested, using if
statement:
if let h = hour, h >= 17 {
return true
} else {
return false
}
Or else, simply:
return hour != nil && hour! >= 17
The error message is often found in this pattern:
enum MyEnum {
case patternA(Int)
case patternB(Int)
case patternC
}
let me = MyEnum.patternB(30)
switch me {
case .patternA(let h), .patternB(let h), .patternC: //<-
print("A or B with h, or C")
default:
print("this may never happen")
}
I tried this when I got the same error:
case .something, .other(let parameter) where parameter != nil:
doSomething()
I think, Swift 5 can't handle such complicated expressions, so I had to split it:
case .something:
doSomething()
case .other(let parameter) where parameter != nil:
doSomething()