LoginSignup
5
0

More than 5 years have passed since last update.

三項演算子のハテナ"?"

Last updated at Posted at 2018-10-19

swiftでは、ハテナ"?"をオプショナル型を用いる時に利用するが、それ以外にも三項演算子として用いることがある。三項演算子を使うと、if文よりも短く条件分岐が書ける。以下に例を示す。

if文で表すと
var a = 0

if a == 0 {
    a = 1
} else {
    a = 0
}
三項演算子?で表すと
a = a == 0 ? 1 : 0

綺麗だ!!!!

5
0
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
5
0