LoginSignup
0
1

More than 3 years have passed since last update.

guard文まとめてみた

Posted at

guard文の役割

想定外の状況が起こった時にその処理から抜け出す役割をします。

書き方

guard 条件 else {
   条件を満たさなかったときの処理
   break //returnでも良い
} 条件を満たした時の処理

使ってみた

number2を割る数として割る数に0が入った時、{}内の処理が行われます。
条件式 number2 != 0の時{}外の処理
number2 == 0の時{}内の処理です。

            guard number2 != 0 else{
                answerLabel.text = "割る数には0以外の数字を入れてください"
                return
            }
            kekka = number1 / number2

参考文献

詳解Swift 第5版

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1