0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Swift】オプショナル型の何が嬉しいのか

Posted at

何が嬉しいのか

nilを参照することができる。
つまり、例えば if とかで「もしnilならば」という条件を使うことができる。

定義してみる

先に正解を言っておくと

var hoge: String?

print(hoge)

// 出力は nil

こう書けば nil が出力される。
しかし ? を付けずに書いたり

var hoge: String

print(hoge)

// エラー

こう書いたりしてもエラーになる。

var hoge: String

hoge = nil

print(hoge)

// エラー

paiza.ioとかで気軽に試すことができる。
次回はこのオプショナル型の hoge を活用する方法を書く。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?