Swift 2 と Xcode 7 を使用しています。
の違いを知りたい
if condition { ... } else { ... }
と
guard ... else ...
Swift 2 と Xcode 7 を使用しています。
の違いを知りたい
if condition { ... } else { ... }
と
guard ... else ...
To add to Matt's answer, you can include several conditions in a single guard statement:
guard let x = xOptional, y = yOptional else { return }
// ... and now x and y are in scope _at top level_
In addition to optional binding, a guard condition can test boolean results:
guard x > 0 else { return }
In short, the benefit of the guard statement is to make the early exit apparent at the start of the scope, instead of the condition being buried further down in a nested else statement.