Last active
August 29, 2015 14:25
-
-
Save haranicle/d52aaa62595ad138b539 to your computer and use it in GitHub Desktop.
guardについて #cswift #CodePiece
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
guard let value != 0 else { | |
// ここでスコープを抜ける処理 | |
return | |
break | |
continue | |
fatalError() | |
} | |
// 上の条件が満たされていることを前提としたコードが書ける | |
print( 100 / value ) | |
// きもいけどこんなふうに書ける | |
guard let path = getPath() where path.hasPrefix("hoge") { | |
fatalError() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1 行目の補足ですけど
guard let value != 0 else {
の場合は let なしです。let を使うときは、新しい変数で受けたいとき、たとえば Optional Binding で
guard let value = optionalValue else {
としたい場合などです。