-4

Swift 2 と Xcode 7 を使用しています。

の違いを知りたい

if condition { ... } else { ... } 

guard ... else ...
4

3 に答える 3

0

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.

于 2015-11-30T20:50:41.400 に答える