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?

More than 1 year has passed since last update.

[Swift] 出力はOptionalになってるのにCannot force unwrap value of non-optional type '' で怒られる時

Posted at

出力させるとOptionalになってるのに!を入れるとエラーが起こる。

SwiftのOptionalには以下の素晴らしい記事を読もう
https://qiita.com/maiki055/items/b24378a3707bd35a31a8

つまりは、nilを扱う場合に使うやつ。でもそのままStringとかに入れるとOptionalも出てきてしまう。

基本的には、unwrapするときは対象の変数の後ろに"!"を入れてやればいいが以下の場合ではそうはいかなかった。

該当箇所

label2.text = String(describing: result?.data[indexPath.row])

正解の方法

label2.text = String(describing: (result?.data[indexPath.row])!)

"!"をつけるだけでなく()で囲ってやる必要があった。

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?