質問編集履歴

1

きちんと全てのコードを載せました。

2020/04/08 10:35

投稿

Kumt
Kumt

スコア4

test CHANGED
File without changes
test CHANGED
@@ -4,9 +4,239 @@
4
4
 
5
5
 
6
6
 
7
-
7
+ ```StartViewController
8
+
8
-
9
+ class StartViewController: UIViewController {
10
+
11
+
12
+
13
+ override func viewDidLoad(){
14
+
15
+ super.viewDidLoad()
16
+
17
+ }
18
+
19
+
20
+
21
+ override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
22
+
23
+ // 問題文の読み込み
24
+
25
+ QuestionDataManager.sharedInstance.loadQuestion()
26
+
27
+ // 変位先画面の呼び出し
28
+
29
+ guard let nextViewController = segue.destination as?
30
+
31
+ QuestionViewController else {
32
+
33
+ return
34
+
35
+ }
36
+
37
+ guard let questionData = QuestionDataManager.sharedInstance.nextQuestion() else {
38
+
39
+ return
40
+
41
+ }
42
+
43
+ nextViewController.questionData = questionData
44
+
45
+ }
46
+
47
+ @IBAction func goToTitle(_ segue: UIStoryboardSegue) {
48
+
49
+
50
+
51
+ }
52
+
53
+ }
54
+
55
+
56
+
57
+ ```
58
+
59
+
60
+
61
+
62
+
63
+
64
+
65
+ ```QuestionDataManager
66
+
67
+ import Foundation
68
+
69
+
70
+
71
+ class QuestionData {
72
+
73
+
74
+
75
+ var question: String
76
+
77
+ var answer1: String
78
+
79
+ var answer2: String
80
+
81
+ var answer3: String
82
+
83
+ var answer4: String
84
+
85
+ var correctAnswerNumber: Int
86
+
87
+ var userChoiceAnswerNumber: Int?
88
+
89
+ var questionNo: Int = 0
90
+
91
+
92
+
93
+ init(questionSourceDataArray: [String]) {
94
+
95
+ question = questionSourceDataArray[0]
96
+
97
+ answer1 = questionSourceDataArray[1]
98
+
99
+ answer2 = questionSourceDataArray[2]
100
+
101
+ answer3 = questionSourceDataArray[3]
102
+
103
+ answer4 = questionSourceDataArray[4]
104
+
105
+ correctAnswerNumber = Int(questionSourceDataArray[5])!
106
+
107
+ }
108
+
109
+ func isCorrect() -> Bool {
110
+
111
+ if correctAnswerNumber == userChoiceAnswerNumber {
112
+
9
- ```ここに言語を入力
113
+ return true
114
+
115
+ }
116
+
117
+ return false
118
+
119
+ }
120
+
121
+ }
122
+
123
+
124
+
125
+
126
+
127
+
128
+
129
+
130
+
131
+ class QuestionDataManager {
132
+
133
+ static let sharedInstance = QuestionDataManager()
134
+
135
+ var questionDataArray = [QuestionData]()
136
+
137
+ var nowQuestionIndex: Int = 0
138
+
139
+ private init() {
140
+
141
+
142
+
143
+ }
144
+
145
+ func loadQuestion() {
146
+
147
+ questionDataArray.removeAll()
148
+
149
+ nowQuestionIndex = 0
150
+
151
+
152
+
153
+ guard let csvFilePath = Bundle.main.path(forResource: "question", ofType: "csv") else {
154
+
155
+ print("csvファイルが存在しません")
156
+
157
+ return
158
+
159
+ }
160
+
161
+
162
+
163
+ // csvファイル読み込み
164
+
165
+ do {
166
+
167
+ let csvStringData = try String(contentsOfFile: csvFilePath,encoding: String.Encoding.utf8)
168
+
169
+ // csvファイルを1行ずつ読み込む
170
+
171
+ csvStringData.enumerateLines(invoking: { (line, stop) in
172
+
173
+ //カンマ区切りで分割
174
+
175
+ let questionSourceDataArray = line.components(separatedBy: ",")
176
+
177
+ //問題データを格納するオブジェクトを作成
178
+
179
+
180
+
181
+ let questionData = QuestionData(questionSourceDataArray: questionSourceDataArray)
182
+
183
+
184
+
185
+ // 問題を追加
186
+
187
+ self.questionDataArray.append(questionData)
188
+
189
+ // 問題番号を設定
190
+
191
+ questionData.questionNo = self.questionDataArray.count
192
+
193
+ }
194
+
195
+ )
196
+
197
+ } catch let error {
198
+
199
+ print("csvファイル読み込みエラーが発生しました:(error)")
200
+
201
+ return
202
+
203
+ }
204
+
205
+ }
206
+
207
+
208
+
209
+
210
+
211
+ //次の問題を取り出す
212
+
213
+ func nextQuestion() -> QuestionData? {
214
+
215
+ if nowQuestionIndex < questionDataArray.count {
216
+
217
+ let nextQuestion = questionDataArray[nowQuestionIndex]
218
+
219
+ nowQuestionIndex += 1
220
+
221
+ return nextQuestion
222
+
223
+ }
224
+
225
+ return nil
226
+
227
+ }
228
+
229
+ }
230
+
231
+
232
+
233
+
234
+
235
+ ```
236
+
237
+
238
+
239
+ ```QuestionViewController
10
240
 
11
241
  import UIKit
12
242