3
3

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 5 years have passed since last update.

delegate で unexpectedly found nil while unwrapping an optional value が出る時

Posted at

取り急ぎメモ代わりに。

以下のようにdelegateを実装して実行しようとするとエラーが発生し足踏みしていた

protocol CalendarViewDelegate {
    func tapDay(dayText: NSString)
}



// とあるメソッドの中身の以下の行でエラー
self.delegate.tapDay("aaaaa")

エラーメッセージ

unexpectedly found nil while unwrapping an optional value

解決策はシンプルで

self.delegate?.tapDay("aaaaa")

とすれば良い。引数がない場合は?が要らないし、こういう場合はXCodeがコンパイル前にワーニングなりエラーにしてくれたりするから全然分からなかった。

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?