質問編集履歴
4
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -114,6 +114,8 @@
|
|
114
114
|
```
|
115
115
|
スタート画面
|
116
116
|
```
|
117
|
+
import UIKit
|
118
|
+
|
117
119
|
class StartViewController: UIViewController {
|
118
120
|
|
119
121
|
override func viewDidLoad() {
|
@@ -140,6 +142,10 @@
|
|
140
142
|
}
|
141
143
|
nextViewController.questionData = questionData
|
142
144
|
}
|
145
|
+
|
146
|
+
@IBAction func goToTitle(_ segue: UIStoryboardSegue){
|
147
|
+
}
|
148
|
+
}
|
143
149
|
```
|
144
150
|
データベース
|
145
151
|
```
|
3
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -23,7 +23,24 @@
|
|
23
23
|
|
24
24
|
問題画面
|
25
25
|
```swift
|
26
|
+
import UIKit
|
27
|
+
import AudioToolbox
|
28
|
+
|
29
|
+
|
30
|
+
class QuestionViewController: UIViewController{
|
31
|
+
|
32
|
+
var questionData: QuestionData!
|
33
|
+
|
34
|
+
@IBOutlet weak var questionNoLabel: UILabel!
|
35
|
+
@IBOutlet weak var questionTextView: UITextView!
|
36
|
+
@IBOutlet weak var answer1Button: UIButton!
|
37
|
+
@IBOutlet weak var answer2Button: UIButton!
|
38
|
+
@IBOutlet weak var answer3Button: UIButton!
|
39
|
+
@IBOutlet weak var answer4Button: UIButton!
|
40
|
+
@IBOutlet weak var correctImageView: UIImageView!
|
41
|
+
@IBOutlet weak var incorrectImageView: UIImageView!
|
42
|
+
|
26
|
-
override func viewDidLoad() {
|
43
|
+
override func viewDidLoad() {
|
27
44
|
super.viewDidLoad()
|
28
45
|
|
29
46
|
questionNoLabel.text = "Q.(questionData.questionNo)"
|
@@ -33,6 +50,67 @@
|
|
33
50
|
answer3Button.setTitle(questionData.answer3,for: UIControlState.normal)
|
34
51
|
answer4Button.setTitle(questionData.answer4,for: UIControlState.normal)
|
35
52
|
}
|
53
|
+
|
54
|
+
override func didReceiveMemoryWarning() {
|
55
|
+
super.didReceiveMemoryWarning()
|
56
|
+
}
|
57
|
+
@IBAction func tapAnswer1Button(_ sender: Any) {
|
58
|
+
questionData.userChoiceAnswerNumber = 1
|
59
|
+
goNextQuestionWithAnimation()
|
60
|
+
}
|
61
|
+
@IBAction func tapAnswer2Button(_ sender: Any){
|
62
|
+
questionData.userChoiceAnswerNumber = 2
|
63
|
+
goNextQuestionWithAnimation()
|
64
|
+
}
|
65
|
+
|
66
|
+
@IBAction func tapAnswer3Button(_ sender: Any) {
|
67
|
+
questionData.userChoiceAnswerNumber = 3
|
68
|
+
goNextQuestionWithAnimation()
|
69
|
+
}
|
70
|
+
|
71
|
+
@IBAction func tapAnswer4Button(_ sender: Any) {
|
72
|
+
questionData.userChoiceAnswerNumber = 4
|
73
|
+
goNextQuestionWithAnimation()
|
74
|
+
|
75
|
+
}
|
76
|
+
func goNextQuestionWithAnimation(){
|
77
|
+
if questionData.isCorrect(){
|
78
|
+
goNextQuestionWithCorrectAnimation()
|
79
|
+
} else{
|
80
|
+
goNextQuestionWithInCorrectAnimation()
|
81
|
+
}
|
82
|
+
}
|
83
|
+
func goNextQuestionWithCorrectAnimation(){
|
84
|
+
AudioServicesPlayAlertSound(1025)
|
85
|
+
UIView.animate(withDuration: 2.0, animations:{
|
86
|
+
self.correctImageView.alpha = 1.0
|
87
|
+
}){(Bool) in self.goNextQuestion()
|
88
|
+
}
|
89
|
+
}
|
90
|
+
func goNextQuestionWithInCorrectAnimation(){
|
91
|
+
AudioServicesPlayAlertSound(1006)
|
92
|
+
UIView.animate(withDuration: 2.0, animations:{
|
93
|
+
self.correctImageView.alpha = 1.0
|
94
|
+
}){(Bool) in self.goNextQuestion()
|
95
|
+
}
|
96
|
+
}
|
97
|
+
func goNextQuestion(){
|
98
|
+
guard let nextQuestion = QuestionDataManager.sharedInstance.nextQuestion() else{
|
99
|
+
if let resultViewController = storyboard?.instantiateViewController(withIdentifier: "result") as? ResultViewController{
|
100
|
+
present(resultViewController, animated: true, completion: nil)
|
101
|
+
}
|
102
|
+
return
|
103
|
+
}
|
104
|
+
if let nextQuestionViewController = storyboard?.instantiateViewController(withIdentifier: "question")
|
105
|
+
as? QuestionViewController{
|
106
|
+
|
107
|
+
nextQuestionViewController.questionData = nextQuestion
|
108
|
+
present(nextQuestionViewController, animated: true,completion: nil)
|
109
|
+
}
|
110
|
+
}
|
111
|
+
}
|
112
|
+
|
113
|
+
|
36
114
|
```
|
37
115
|
スタート画面
|
38
116
|
```
|
2
修正いたしました
title
CHANGED
File without changes
|
body
CHANGED
@@ -148,7 +148,7 @@
|
|
148
148
|
https://yutahand.com/coffeequiz/
|
149
149
|
|
150
150
|
またこのブログを参考に自分でナンバーを設定もしたのですおなじようなエラーが出てしまいます。
|
151
|
-
またこのブログでは
|
151
|
+
またこのブログではバグと書いてあり、再起動で治ると書いてありましたが再起動しても治りませんでした。
|
152
152
|
### 補足情報(FW/ツールのバージョンなど)
|
153
153
|
|
154
154
|
swift 3
|
1
修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
オプショナル型の変数をアンラップした時にnilが出て、アプリが動かなくなってしまいます
|
1
|
+
オプショナル型の変数をアンラップした時にnilが出て、アプリが動かなくなってしまい困っています
|
body
CHANGED
@@ -19,7 +19,9 @@
|
|
19
19
|
```
|
20
20
|
|
21
21
|
### 該当のソースコード
|
22
|
-
|
22
|
+
問題画面とスタート画面とデータベースのコードを置いておきます。よろしくお願いいたします。
|
23
|
+
|
24
|
+
問題画面
|
23
25
|
```swift
|
24
26
|
override func viewDidLoad() {
|
25
27
|
super.viewDidLoad()
|
@@ -60,10 +62,86 @@
|
|
60
62
|
}
|
61
63
|
nextViewController.questionData = questionData
|
62
64
|
}
|
65
|
+
```
|
66
|
+
データベース
|
67
|
+
```
|
68
|
+
import Foundation
|
69
|
+
|
70
|
+
class QuestionData {
|
71
|
+
var question: String
|
72
|
+
var answer1: String
|
73
|
+
var answer2: String
|
74
|
+
var answer3: String
|
75
|
+
var answer4: String
|
63
76
|
|
77
|
+
var correctAnswerNumber: Int
|
78
|
+
|
79
|
+
var userChoiceAnswerNumber: Int?
|
80
|
+
var questionNo: Int = 0
|
81
|
+
|
82
|
+
init(questionSourceDataArray: [String]){
|
83
|
+
question = questionSourceDataArray[0]
|
84
|
+
answer1 = questionSourceDataArray[1]
|
85
|
+
answer2 = questionSourceDataArray[2]
|
86
|
+
answer3 = questionSourceDataArray[3]
|
87
|
+
answer4 = questionSourceDataArray[4]
|
88
|
+
correctAnswerNumber = Int(questionSourceDataArray[5])!
|
89
|
+
}
|
90
|
+
|
91
|
+
func isCorrect() -> Bool{
|
92
|
+
if correctAnswerNumber == userChoiceAnswerNumber{
|
93
|
+
return true
|
94
|
+
}
|
95
|
+
return false
|
96
|
+
}
|
97
|
+
}
|
64
98
|
|
99
|
+
class QuestionDataManager{
|
100
|
+
static let sharedInstance = QuestionDataManager()
|
101
|
+
|
102
|
+
var questionDataArray = [QuestionData]()
|
103
|
+
|
104
|
+
var nowQuestionIndex: Int = 0
|
105
|
+
private init(){
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
func loadQuestion(){
|
110
|
+
questionDataArray.removeAll()
|
111
|
+
|
112
|
+
nowQuestionIndex = 0
|
113
|
+
|
114
|
+
guard let csvFilePath = Bundle.main.path(forResource: "question",
|
115
|
+
ofType: "csv") else {
|
116
|
+
print("csvファイルが存在しません")
|
117
|
+
return
|
118
|
+
}
|
119
|
+
do {
|
120
|
+
let csvStringData = try String(contentsOfFile: csvFilePath,
|
121
|
+
encoding: String.Encoding.utf8)
|
122
|
+
csvStringData.enumerateLines(invoking:{ (line, stop) in
|
123
|
+
let questionSourceDataArray = line.components(separatedBy: ",")
|
124
|
+
let questionData = QuestionData (questionSourceDataArray:
|
125
|
+
questionSourceDataArray)
|
126
|
+
self.questionDataArray.append(questionData)
|
127
|
+
questionData.questionNo = self.questionDataArray.count
|
128
|
+
})
|
129
|
+
} catch let error{
|
130
|
+
print("csvファイル読み込みエラーが発生しました:(error)")
|
131
|
+
return
|
132
|
+
}
|
133
|
+
}
|
134
|
+
func nextQuestion() -> QuestionData? {
|
135
|
+
if nowQuestionIndex < questionDataArray.count {
|
136
|
+
let nextQuestion = questionDataArray[nowQuestionIndex]
|
137
|
+
nowQuestionIndex += 1
|
138
|
+
return nextQuestion
|
139
|
+
}
|
140
|
+
return nil
|
141
|
+
}
|
142
|
+
}
|
143
|
+
|
65
144
|
```
|
66
|
-
|
67
145
|
### 試したこと
|
68
146
|
読み込み部分のコードでおかしい部分がないか確認しましたが間違えていないと思います。。
|
69
147
|
|