質問編集履歴
1
きちんと全てのコードを載せました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,8 +1,123 @@
|
|
1
1
|
画面を開くと下記の部分でこのエラーが発生します。
|
2
2
|
QuestionLabel.text = "Q. (questionData.questionNo)"
|
3
3
|
|
4
|
+
```StartViewController
|
5
|
+
class StartViewController: UIViewController {
|
6
|
+
|
7
|
+
override func viewDidLoad(){
|
8
|
+
super.viewDidLoad()
|
9
|
+
}
|
10
|
+
|
11
|
+
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
12
|
+
// 問題文の読み込み
|
13
|
+
QuestionDataManager.sharedInstance.loadQuestion()
|
14
|
+
// 変位先画面の呼び出し
|
15
|
+
guard let nextViewController = segue.destination as?
|
16
|
+
QuestionViewController else {
|
17
|
+
return
|
18
|
+
}
|
19
|
+
guard let questionData = QuestionDataManager.sharedInstance.nextQuestion() else {
|
20
|
+
return
|
21
|
+
}
|
22
|
+
nextViewController.questionData = questionData
|
23
|
+
}
|
24
|
+
@IBAction func goToTitle(_ segue: UIStoryboardSegue) {
|
25
|
+
|
26
|
+
}
|
27
|
+
}
|
4
28
|
|
29
|
+
```
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
```QuestionDataManager
|
34
|
+
import Foundation
|
35
|
+
|
36
|
+
class QuestionData {
|
37
|
+
|
38
|
+
var question: String
|
39
|
+
var answer1: String
|
40
|
+
var answer2: String
|
41
|
+
var answer3: String
|
42
|
+
var answer4: String
|
43
|
+
var correctAnswerNumber: Int
|
44
|
+
var userChoiceAnswerNumber: Int?
|
45
|
+
var questionNo: Int = 0
|
46
|
+
|
47
|
+
init(questionSourceDataArray: [String]) {
|
48
|
+
question = questionSourceDataArray[0]
|
49
|
+
answer1 = questionSourceDataArray[1]
|
50
|
+
answer2 = questionSourceDataArray[2]
|
51
|
+
answer3 = questionSourceDataArray[3]
|
52
|
+
answer4 = questionSourceDataArray[4]
|
53
|
+
correctAnswerNumber = Int(questionSourceDataArray[5])!
|
54
|
+
}
|
55
|
+
func isCorrect() -> Bool {
|
56
|
+
if correctAnswerNumber == userChoiceAnswerNumber {
|
5
|
-
|
57
|
+
return true
|
58
|
+
}
|
59
|
+
return false
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
class QuestionDataManager {
|
67
|
+
static let sharedInstance = QuestionDataManager()
|
68
|
+
var questionDataArray = [QuestionData]()
|
69
|
+
var nowQuestionIndex: Int = 0
|
70
|
+
private init() {
|
71
|
+
|
72
|
+
}
|
73
|
+
func loadQuestion() {
|
74
|
+
questionDataArray.removeAll()
|
75
|
+
nowQuestionIndex = 0
|
76
|
+
|
77
|
+
guard let csvFilePath = Bundle.main.path(forResource: "question", ofType: "csv") else {
|
78
|
+
print("csvファイルが存在しません")
|
79
|
+
return
|
80
|
+
}
|
81
|
+
|
82
|
+
// csvファイル読み込み
|
83
|
+
do {
|
84
|
+
let csvStringData = try String(contentsOfFile: csvFilePath,encoding: String.Encoding.utf8)
|
85
|
+
// csvファイルを1行ずつ読み込む
|
86
|
+
csvStringData.enumerateLines(invoking: { (line, stop) in
|
87
|
+
//カンマ区切りで分割
|
88
|
+
let questionSourceDataArray = line.components(separatedBy: ",")
|
89
|
+
//問題データを格納するオブジェクトを作成
|
90
|
+
|
91
|
+
let questionData = QuestionData(questionSourceDataArray: questionSourceDataArray)
|
92
|
+
|
93
|
+
// 問題を追加
|
94
|
+
self.questionDataArray.append(questionData)
|
95
|
+
// 問題番号を設定
|
96
|
+
questionData.questionNo = self.questionDataArray.count
|
97
|
+
}
|
98
|
+
)
|
99
|
+
} catch let error {
|
100
|
+
print("csvファイル読み込みエラーが発生しました:(error)")
|
101
|
+
return
|
102
|
+
}
|
103
|
+
}
|
104
|
+
|
105
|
+
|
106
|
+
//次の問題を取り出す
|
107
|
+
func nextQuestion() -> QuestionData? {
|
108
|
+
if nowQuestionIndex < questionDataArray.count {
|
109
|
+
let nextQuestion = questionDataArray[nowQuestionIndex]
|
110
|
+
nowQuestionIndex += 1
|
111
|
+
return nextQuestion
|
112
|
+
}
|
113
|
+
return nil
|
114
|
+
}
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
```
|
119
|
+
|
120
|
+
```QuestionViewController
|
6
121
|
import UIKit
|
7
122
|
import AudioToolbox
|
8
123
|
|