質問編集履歴
4
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -230,6 +230,10 @@
|
|
230
230
|
|
231
231
|
```
|
232
232
|
|
233
|
+
import UIKit
|
234
|
+
|
235
|
+
|
236
|
+
|
233
237
|
class StartViewController: UIViewController {
|
234
238
|
|
235
239
|
|
@@ -282,6 +286,14 @@
|
|
282
286
|
|
283
287
|
}
|
284
288
|
|
289
|
+
|
290
|
+
|
291
|
+
@IBAction func goToTitle(_ segue: UIStoryboardSegue){
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
}
|
296
|
+
|
285
297
|
```
|
286
298
|
|
287
299
|
データベース
|
3
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -48,7 +48,41 @@
|
|
48
48
|
|
49
49
|
```swift
|
50
50
|
|
51
|
+
import UIKit
|
52
|
+
|
53
|
+
import AudioToolbox
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
class QuestionViewController: UIViewController{
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
var questionData: QuestionData!
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
@IBOutlet weak var questionNoLabel: UILabel!
|
68
|
+
|
69
|
+
@IBOutlet weak var questionTextView: UITextView!
|
70
|
+
|
71
|
+
@IBOutlet weak var answer1Button: UIButton!
|
72
|
+
|
73
|
+
@IBOutlet weak var answer2Button: UIButton!
|
74
|
+
|
75
|
+
@IBOutlet weak var answer3Button: UIButton!
|
76
|
+
|
77
|
+
@IBOutlet weak var answer4Button: UIButton!
|
78
|
+
|
79
|
+
@IBOutlet weak var correctImageView: UIImageView!
|
80
|
+
|
81
|
+
@IBOutlet weak var incorrectImageView: UIImageView!
|
82
|
+
|
83
|
+
|
84
|
+
|
51
|
-
override func viewDidLoad() {
|
85
|
+
override func viewDidLoad() {
|
52
86
|
|
53
87
|
super.viewDidLoad()
|
54
88
|
|
@@ -68,6 +102,128 @@
|
|
68
102
|
|
69
103
|
}
|
70
104
|
|
105
|
+
|
106
|
+
|
107
|
+
override func didReceiveMemoryWarning() {
|
108
|
+
|
109
|
+
super.didReceiveMemoryWarning()
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
@IBAction func tapAnswer1Button(_ sender: Any) {
|
114
|
+
|
115
|
+
questionData.userChoiceAnswerNumber = 1
|
116
|
+
|
117
|
+
goNextQuestionWithAnimation()
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
@IBAction func tapAnswer2Button(_ sender: Any){
|
122
|
+
|
123
|
+
questionData.userChoiceAnswerNumber = 2
|
124
|
+
|
125
|
+
goNextQuestionWithAnimation()
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
@IBAction func tapAnswer3Button(_ sender: Any) {
|
132
|
+
|
133
|
+
questionData.userChoiceAnswerNumber = 3
|
134
|
+
|
135
|
+
goNextQuestionWithAnimation()
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
@IBAction func tapAnswer4Button(_ sender: Any) {
|
142
|
+
|
143
|
+
questionData.userChoiceAnswerNumber = 4
|
144
|
+
|
145
|
+
goNextQuestionWithAnimation()
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
func goNextQuestionWithAnimation(){
|
152
|
+
|
153
|
+
if questionData.isCorrect(){
|
154
|
+
|
155
|
+
goNextQuestionWithCorrectAnimation()
|
156
|
+
|
157
|
+
} else{
|
158
|
+
|
159
|
+
goNextQuestionWithInCorrectAnimation()
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
func goNextQuestionWithCorrectAnimation(){
|
166
|
+
|
167
|
+
AudioServicesPlayAlertSound(1025)
|
168
|
+
|
169
|
+
UIView.animate(withDuration: 2.0, animations:{
|
170
|
+
|
171
|
+
self.correctImageView.alpha = 1.0
|
172
|
+
|
173
|
+
}){(Bool) in self.goNextQuestion()
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
func goNextQuestionWithInCorrectAnimation(){
|
180
|
+
|
181
|
+
AudioServicesPlayAlertSound(1006)
|
182
|
+
|
183
|
+
UIView.animate(withDuration: 2.0, animations:{
|
184
|
+
|
185
|
+
self.correctImageView.alpha = 1.0
|
186
|
+
|
187
|
+
}){(Bool) in self.goNextQuestion()
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
func goNextQuestion(){
|
194
|
+
|
195
|
+
guard let nextQuestion = QuestionDataManager.sharedInstance.nextQuestion() else{
|
196
|
+
|
197
|
+
if let resultViewController = storyboard?.instantiateViewController(withIdentifier: "result") as? ResultViewController{
|
198
|
+
|
199
|
+
present(resultViewController, animated: true, completion: nil)
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
return
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
if let nextQuestionViewController = storyboard?.instantiateViewController(withIdentifier: "question")
|
208
|
+
|
209
|
+
as? QuestionViewController{
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
nextQuestionViewController.questionData = nextQuestion
|
214
|
+
|
215
|
+
present(nextQuestionViewController, animated: true,completion: nil)
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
|
226
|
+
|
71
227
|
```
|
72
228
|
|
73
229
|
スタート画面
|
2
修正いたしました
test
CHANGED
File without changes
|
test
CHANGED
@@ -298,7 +298,7 @@
|
|
298
298
|
|
299
299
|
またこのブログを参考に自分でナンバーを設定もしたのですおなじようなエラーが出てしまいます。
|
300
300
|
|
301
|
-
またこのブログでは
|
301
|
+
またこのブログではバグと書いてあり、再起動で治ると書いてありましたが再起動しても治りませんでした。
|
302
302
|
|
303
303
|
### 補足情報(FW/ツールのバージョンなど)
|
304
304
|
|
1
修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
オプショナル型の変数をアンラップした時にnilが出て、アプリが動かなくなってしまいます
|
1
|
+
オプショナル型の変数をアンラップした時にnilが出て、アプリが動かなくなってしまい困っています
|
test
CHANGED
@@ -40,7 +40,11 @@
|
|
40
40
|
|
41
41
|
### 該当のソースコード
|
42
42
|
|
43
|
-
|
43
|
+
問題画面とスタート画面とデータベースのコードを置いておきます。よろしくお願いいたします。
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
問題画面
|
44
48
|
|
45
49
|
```swift
|
46
50
|
|
@@ -122,13 +126,165 @@
|
|
122
126
|
|
123
127
|
}
|
124
128
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
```
|
129
|
+
```
|
130
|
+
|
130
|
-
|
131
|
+
データベース
|
132
|
+
|
131
|
-
|
133
|
+
```
|
134
|
+
|
135
|
+
import Foundation
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
class QuestionData {
|
140
|
+
|
141
|
+
var question: String
|
142
|
+
|
143
|
+
var answer1: String
|
144
|
+
|
145
|
+
var answer2: String
|
146
|
+
|
147
|
+
var answer3: String
|
148
|
+
|
149
|
+
var answer4: String
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
var correctAnswerNumber: Int
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
var userChoiceAnswerNumber: Int?
|
158
|
+
|
159
|
+
var questionNo: Int = 0
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
init(questionSourceDataArray: [String]){
|
164
|
+
|
165
|
+
question = questionSourceDataArray[0]
|
166
|
+
|
167
|
+
answer1 = questionSourceDataArray[1]
|
168
|
+
|
169
|
+
answer2 = questionSourceDataArray[2]
|
170
|
+
|
171
|
+
answer3 = questionSourceDataArray[3]
|
172
|
+
|
173
|
+
answer4 = questionSourceDataArray[4]
|
174
|
+
|
175
|
+
correctAnswerNumber = Int(questionSourceDataArray[5])!
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
func isCorrect() -> Bool{
|
182
|
+
|
183
|
+
if correctAnswerNumber == userChoiceAnswerNumber{
|
184
|
+
|
185
|
+
return true
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
return false
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
class QuestionDataManager{
|
198
|
+
|
199
|
+
static let sharedInstance = QuestionDataManager()
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
var questionDataArray = [QuestionData]()
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
var nowQuestionIndex: Int = 0
|
208
|
+
|
209
|
+
private init(){
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
func loadQuestion(){
|
218
|
+
|
219
|
+
questionDataArray.removeAll()
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
nowQuestionIndex = 0
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
guard let csvFilePath = Bundle.main.path(forResource: "question",
|
228
|
+
|
229
|
+
ofType: "csv") else {
|
230
|
+
|
231
|
+
print("csvファイルが存在しません")
|
232
|
+
|
233
|
+
return
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
do {
|
238
|
+
|
239
|
+
let csvStringData = try String(contentsOfFile: csvFilePath,
|
240
|
+
|
241
|
+
encoding: String.Encoding.utf8)
|
242
|
+
|
243
|
+
csvStringData.enumerateLines(invoking:{ (line, stop) in
|
244
|
+
|
245
|
+
let questionSourceDataArray = line.components(separatedBy: ",")
|
246
|
+
|
247
|
+
let questionData = QuestionData (questionSourceDataArray:
|
248
|
+
|
249
|
+
questionSourceDataArray)
|
250
|
+
|
251
|
+
self.questionDataArray.append(questionData)
|
252
|
+
|
253
|
+
questionData.questionNo = self.questionDataArray.count
|
254
|
+
|
255
|
+
})
|
256
|
+
|
257
|
+
} catch let error{
|
258
|
+
|
259
|
+
print("csvファイル読み込みエラーが発生しました:(error)")
|
260
|
+
|
261
|
+
return
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
}
|
266
|
+
|
267
|
+
func nextQuestion() -> QuestionData? {
|
268
|
+
|
269
|
+
if nowQuestionIndex < questionDataArray.count {
|
270
|
+
|
271
|
+
let nextQuestion = questionDataArray[nowQuestionIndex]
|
272
|
+
|
273
|
+
nowQuestionIndex += 1
|
274
|
+
|
275
|
+
return nextQuestion
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
return nil
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
```
|
132
288
|
|
133
289
|
### 試したこと
|
134
290
|
|