반응형
신속한 사건
신속 함이 성명을 넘어서는가? 예를 들어 내가 다음을 수행하는 경우
var testVar = "hello"
var result = 0
switch(testVal)
{
case "one":
result = 1
case "two":
result = 1
default:
result = 3
}
"one"사례와 "two"사례에 대해 동일한 코드를 실행할 수 있습니까?
예. 다음과 같이 할 수 있습니다 :
var testVal = "hello"
var result = 0
switch testVal {
case "one", "two":
result = 1
default:
result = 3
}
또는 다음 fallthrough
키워드를 사용할 수 있습니다 .
var testVal = "hello"
var result = 0
switch testVal {
case "one":
fallthrough
case "two":
result = 1
default:
result = 3
}
var testVar = "hello"
switch(testVar) {
case "hello":
println("hello match number 1")
fallthrough
case "two":
println("two in not hello however the above fallthrough automatically always picks the case following whether there is a match or not! To me this is wrong")
default:
println("Default")
}
case "one", "two":
result = 1
break statement는 없지만 사례가 훨씬 유연합니다.
부록 : Analog File이 지적했듯이 break
Swift 에는 실제로 진술이 있습니다. switch
빈 케이스가 허용되지 않으므로 빈 케이스를 채울 필요가 없으면 명령문에서 불필요한 경우에도 여전히 루프에서 사용할 수 있습니다. 예를 들면 다음과 같습니다 default: break
..
다음은 이해하기 쉬운 예입니다.
let value = 0
switch value
{
case 0:
print(0) // print 0
fallthrough
case 1:
print(1) // print 1
case 2:
print(2) // Doesn't print
default:
print("default")
}
결론 : fallthrough
이전 사례 fallthrough
가 일치하는지 여부에 따라 다음 사례 (단 하나만)를 실행하는 데 사용 합니다 .
The keyword fallthrough
at the end of a case causes the fall-through behavior you're looking for, and multiple values can be checked in a single case.
참고URL : https://stackoverflow.com/questions/24049024/swift-case-falling-through
반응형
'Programing' 카테고리의 다른 글
Postgres 수동 순서 변경 (0) | 2020.06.16 |
---|---|
숭고한 텍스트 2 여러 줄 편집 (0) | 2020.06.16 |
jquery를 통해 앵커 클릭을 어떻게 시뮬레이트 할 수 있습니까? (0) | 2020.06.16 |
공분산과 역 분산의 차이 (0) | 2020.06.16 |
Chrome 콘솔에서 전체 개체를 표시하는 방법은 무엇입니까? (0) | 2020.06.16 |