Swift/Statements/BranchStatements
(Redirected from Swift/Statements/switch)
処理分岐をかけるためには、if, guard, switch を使用する。
if
if 条件文 {
処理
}
if 条件文 {
処理
} else {
処理
}
if 条件文 {
処理
} else if 条件文 {
処理
} else {
処理
}
guard
guard と同じことを ifでもできるが、処理をシンプルにイリーガル値だった場合に関数抜け出す場合にシンプルにかける。 guard の後の条件文に合致しない場合の処理をelseの後に書く。
func xxx (arg1: Int) -> Int? {
guard arg1 != 0 else { return nil }
....
}
switch
siwtch (control expression) {
case pattern1:
statements1
case pattern2 where condition2:
statements2
case pattern3 where condition3, pattern4 where condition4:
statements3
defaults:
defaultstatements
}
https://qiita.com/akatsuki174/items/2720ebc369a6c1d9f629 が分かりやすかった。