0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

guardを用いたnilクラッシュの回避

0
Last updated at Posted at 2026-01-03

そういえば執筆していなかったSwift基本中の基本

実現する挙動

  • nilによるクラッシュを防ぎ、安全に型を扱う

コード例

// String?型のtextを定義する
guard let nullableString = text else { return }
print(nullableString)

結果

  • text = "0" // 0が表示される
  • text = "" // 空文字列が出力される
  • text = nil // printの処理が行われない
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?