LoginSignup
1
2

More than 5 years have passed since last update.

[Swift]値がnilかどうかで異なる値を返す

Posted at
var nilValue:String? = nil
var result = nilValue ?? "This is nil"

Swiftは基本的にnilが値になる変数を宣言することができませんが、変数の後に?をつけることで、nilを許可することができます。(オプショナル型)
nilが利用出来るため、上記のように「nil演算子( x ?? 返したい値)」が可能となります。

1
2
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
1
2