Why should I create a variable outside of switch/case?
For example this code will have an error Cannot find 'size' in scope:
func sizeCheckNoVar(value: Int) -> String {
switch value {
case 0...2:
let size = "small"
case 3...5:
let size = "medium"
case 6...10:
let size = "large"
default:
let size = "huge"
}
return size
}
There is a default condition and AFIK all options are covered.
In the same time this code will be fine:
func sizeCheckVar(value: Int) -> String {
var size: String
switch value {
case 0...2:
size = "small"
case 3...5:
size = "medium"
case 6...10:
size = "large"
default:
size = "huge"
}
return size
}
PS I saw this question Cannot find variable in scope , but I want to know why instead of how to avoid