[Swift]switch文にfallthroughを使う方法

書式

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
switch 項目 {
case 値:処理コード
  fallthrough
}
switch 項目 { case 値:処理コード   fallthrough }
switch 項目 {
  case 値:処理コード
    fallthrough
}

fallthrough
switch文の中で各ケースの処理の実行が終了し,
fallthroughを実行すると直下のケースの処理を実行することができます.

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let cityID = 206
var res: String = "都市は"
switch cityID {
//範囲指定(range) 200~205
case 200..<206:
print("東京")
case 206:
res += "福岡"
fallthrough
default:
res += " です"
}
print(res)
let cityID = 206 var res: String = "都市は" switch cityID { //範囲指定(range) 200~205 case 200..<206: print("東京") case 206: res += "福岡" fallthrough default: res += " です" } print(res)
let cityID = 206

var res: String = "都市は"
switch cityID {
    //範囲指定(range) 200~205
    case 200..<206:
        print("東京")
      case 206:
        res += "福岡"
        fallthrough  
    default:
        res += " です"
}
print(res)

実行結果
都市は福岡 です

 

Swift

Posted by arkgame