質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

607閲覧

Swift4 Fatal error: Unexpectedly found nil while unwrapping an Optional value について

sueken55

総合スコア8

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/11/12 09:07

Fatal error: Unexpectedly found nil while unwrapping an Optional value

現在swift4を用いて簡単なRSSReaderを作っています。
その際に、不慣れな自分では解決できない問題に当たってしまいました。
tableviewCellを呼び出し、そのLabelViewに代入しようとすると以下のエラーが出てしまいます。

###発生している問題・エラーメッセージ

Fatal error: Unexpectedly found nil while unwrapping an Optional value

###該当のソースコード

テーブル側のコード import UIKit import Alamofire import SwiftyJSON class TableViewController: UITableViewController { var fetchFrom: String? var articlesList: [[String: String]] = [] override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self self.tableView.register(ArticleTableViewCell.self, forCellReuseIdentifier: "ArticleTableViewCellXib") // // tableView.register(UINib(nibName: "ArticleTableViewCell", bundle: nil), forCellReuseIdentifier: "ArticleTableViewCellXib") Alamofire.request(fetchFrom!, parameters: nil).responseJSON{ response in let json = JSON(response.result.value) json["articles"].forEach{(_, article) in let articleInfo: Dictionary<String, String> = ["title": article["title"].string!, "description": article["description"].string!, "url": article["url"].string!, "imageUrl": article["urlToImage"].string!] self.articlesList.append(articleInfo) } self.tableView.reloadData() } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows return self.articlesList.count } override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 90 } //## 問題の箇所 ## override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "ArticleTableViewCellXib", for: indexPath) as! ArticleTableViewCell cell.articleDescription.text = self.articlesList[indexPath.row]["description"] <== ここの代入の箇所でエラーが発生します cell.articleTitle.text = self.articlesList[indexPath.row]["title"] <== ここの代入の箇所でエラーが発生します return cell } }
せる側のコード class ArticleTableViewCell: UITableViewCell { var title: String? var explain: String? @IBOutlet weak var articleTitle: UILabel! @IBOutlet weak var articleDescription: UILabel! override func awakeFromNib() { super.awakeFromNib() print("hello wrold") // Initialization code } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } }

###試したこと
if文を用いてnilかどうかのチェックなど行ったり、
代入予定の値をprintしてみてもnilであったことはなかったです

###補足情報(言語/FW/ツール等のバージョンなど)
皆さんのお知恵をお借りできれば幸いです

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

提示いただいたソースではArticleTableViewCell のarticleTitleとarticleDescriptionがnilのままです。
そのため、該当箇所でエラーが発生しています。
以下のようにしてみてください。

Swift

1 @IBOutlet weak var articleTitle = UILabel() 2 @IBOutlet weak var articleDescription = UILabel()

また、なるべくオプショナル型には!を使わずにguard letやif letを使った方がよいです。

※上記だとStoryBoardが生成するUILabelとは別になってしまうため手動でレイアウト等を設定する必要が発生してしまいます。以下にほかに考えられる原因と思われるものを書きます。

参考サイト:http://xyk.hatenablog.com/entry/2017/01/15/202620
・StoryboardでArticleTableViewCellの設定をしている場合は以下のコードは不要です。

Swift

1self.tableView.register(ArticleTableViewCell.self, forCellReuseIdentifier: "ArticleTableViewCellXib")

投稿2017/11/12 10:29

編集2017/11/12 13:40
nakasho_dev

総合スコア2655

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sueken55

2017/11/12 11:49

ご回答ありがとうございます。エラーは出なくなりました。ありがとうございます。 申し訳ないのですが、もう一つ問題が発生してしまったので質問してもよろしいでしょうか? 値の代入で先のエラーは出なくなったのですが、シュミレータで確認したところarticleTitleとarticleDescriptionが表示されないのです。 もう一度お知恵をお借りできれば幸いです。よろしくお願いします。
sueken55

2017/11/14 01:38

解決できました。ありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問