Swift4:csvファイルを読み込みテキストビューに表示
Xcode(Swift4)でクイズアプリを作っています。
nilに問題があるとまでわかりましたが、解決には至りませんでした。。
ビルドを実行しようとしたさいに以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
該当のソースコード
questionNoLabel.text = "Q.(questionData.questionNo)"の部分
Swift4
1// 2// QuestionViewController.swift 3// financial 4// 5// Created by てつや on 2019/06/09. 6// Copyright © 2019年 tetsuya. All rights reserved. 7// 8 9import UIKit 10import AudioToolbox 11 12class QuestionViewController: UIViewController { 13 14 var questionData: QuestionData! 15 16 @IBOutlet weak var questionNoLabel: UILabel! // 問題番号ラベル 17 @IBOutlet weak var questionTextView: UITextView! //問題文 18 @IBOutlet weak var answer1Button: UIButton! // 選択肢1ボタン 19 @IBOutlet weak var answer2Button: UIButton! // 選択肢2ボタン 20 @IBOutlet weak var answer3Button: UIButton! // 選択肢3ボタン 21 @IBOutlet weak var answer4Button: UIButton! // 選択肢4ボタン 22 23 24 @IBOutlet weak var correctImageView: UIImageView! // 正解時のイメージビュー 25 @IBOutlet weak var incorrectImageView: UIImageView! // 不正解時のイメージビュー 26 27 override func viewDidLoad(){ 28 super.viewDidLoad() 29 //初期データ設定処理。前画面で設定済のquestionDataから値を取り出す 30 questionNoLabel.text = "Q.(questionData.questionNo)" 31 32 questionTextView.text = questionData.question 33 34 answer1Button.setTitle(questionData.answer1, for: UIControlState.normal) 35 answer2Button.setTitle(questionData.answer2, for: UIControlState.normal) 36 answer3Button.setTitle(questionData.answer3, for: UIControlState.normal) 37 answer4Button.setTitle(questionData.answer4, for: UIControlState.normal) 38 } 39 override func didReceiveMemoryWarning() { 40 super.didReceiveMemoryWarning() 41 } 42 43 //選択技1をタップ 44 @IBAction func tapAnswer1Button(_ sender: Any){ 45 questionData.userChoiceAnswerNumber = 1 //選択した答えを保存する 46 goNextQuestionWithAnimation() //次の問題に進む 47 } 48 49 //選択技2をタップ 50 @IBAction func tapAnswer2Button(_ sender: Any){ 51 questionData.userChoiceAnswerNumber = 2 //選択した答えを保存する 52 goNextQuestionWithAnimation() //次の問題に進む 53 } 54 55 //選択技3をタップ 56 @IBAction func tapAnswer3Button(_ sender: Any){ 57 questionData.userChoiceAnswerNumber = 3 //選択した答えを保存する 58 goNextQuestionWithAnimation() //次の問題に進む 59 } 60 61 //選択技4をタップ 62 @IBAction func tapAnswer4Button(_ sender: Any){ 63 questionData.userChoiceAnswerNumber = 4 //選択した答えを保存する 64 goNextQuestionWithAnimation() //次の問題に進む 65 } 66 // 次の問題にアニメーション付きで進む 67 func goNextQuestionWithAnimation() { 68 // 正解しているか判定する 69 if questionData.isCorrect() { 70 // 正解のアニメーションを再生しながら次の問題へ遷移する 71 goNextQuestionWithCorrectAnimation() 72 } else { 73 // 不正解のアニメーションを再生しながら次の問題へ遷移する 74 goNextQuestionWithIncorrectAnimation() 75 } 76 } 77 78 // 次の問題に正解のアニメーション付きで遷移する 79 func goNextQuestionWithCorrectAnimation() { 80 // 正解を伝える音を鳴らす 81 AudioServicesPlayAlertSound(1025) 82 83 // アニメーション 84 UIView.animate(withDuration: 2.0, animations: { 85 // アルファ値を1.0に変化させる(初期値はStoryboardで0.0に設定済み) 86 self.correctImageView.alpha = 1.0 87 }) { (Bool) in 88 self.goNextQuestion() // アニメーション完了後に次の問題に進む 89 } 90 } 91 92 // 次の問題に不正解のアニメーション付きで遷移する 93 func goNextQuestionWithIncorrectAnimation() { 94 // 不正解を伝える音を鳴らす 95 AudioServicesPlayAlertSound(1006) 96 // アニメーション 97 UIView.animate(withDuration: 2.0, animations: { 98 // アルファ値を1.0に変化させる(初期値はStoryboardで0.0に設定済み) 99 self.incorrectImageView.alpha = 1.0 100 }) { (Bool) in 101 self.goNextQuestion() // アニメーション完了後に次の問題に進む 102 } 103 } 104 105 // 次の問題へ遷移する 106 func goNextQuestion() { 107 // 問題文の取り出し 108 guard let nextQuestion = 109 QuestionDataManager.sharedInstance.nextQuestion() else { 110 // 問題文がなければ結果画面へ遷移する 111 // StoryboardのIdentifierに設定した値(result)を指定して 112 // ViewControllerを生成する 113 if let resultViewController = storyboard?.instantiateViewController(withIdentifier: "result") as? ResultViewController { 114 // StoryboardのSegueを利用しない明示的な画面遷移処理 115 present(resultViewController, animated: true, completion: nil) 116 } 117 return 118 } 119 // 問題文がある場合は次の問題へ遷移する 120 // StoryboardのIdentifierに設定した値(question)を設定して 121 // ViewControllerを生成する 122 if let nextQuestionViewController = 123 storyboard?.instantiateViewController(withIdentifier: "question") 124 as? QuestionViewController { 125 nextQuestionViewController.questionData = nextQuestion 126 // StoryboardのSegueを利用しない明示的な画面遷移処理 127 present(nextQuestionViewController, animated: true, 128 completion: nil) 129 } 130 } 131} 132
試したこと
・部品接続の確認
・コードの間違え探し
補足情報(FW/ツールのバージョンなど)
なし
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。