質問編集履歴

1

storyboard、前後のコードの一部を追加しました。

2021/12/10 10:54

投稿

Gameno
Gameno

スコア10

test CHANGED
File without changes
test CHANGED
@@ -39,3 +39,341 @@
39
39
 
40
40
 
41
41
  よろしくお願いします。
42
+
43
+
44
+
45
+ storyboard
46
+
47
+ ![イメージ説明](e857f1da12e52ff8ac641cbc072cde49.png)
48
+
49
+
50
+
51
+ 青い画面のコード
52
+
53
+ ```Swift
54
+
55
+ import UIKit
56
+
57
+ import RealmSwift
58
+
59
+
60
+
61
+ class ViewController: UIViewController {
62
+
63
+
64
+
65
+ var startCount: Int = 0
66
+
67
+ let userDefaults = UserDefaults.standard
68
+
69
+
70
+
71
+
72
+
73
+ override func viewDidLoad() {
74
+
75
+ super.viewDidLoad()
76
+
77
+
78
+
79
+ startCount = userDefaults.integer(forKey: "startCount")
80
+
81
+ if startCount == 0 {
82
+
83
+ // 完全初期化
84
+
85
+ var config = Realm.Configuration()
86
+
87
+ config.deleteRealmIfMigrationNeeded = true
88
+
89
+ let realm = try! Realm(configuration: config)
90
+
91
+ try! realm.write {
92
+
93
+ realm.deleteAll()
94
+
95
+ }
96
+
97
+
98
+
99
+ //category.csvの読み込みとrealm化
100
+
101
+ guard let categoryPath = Bundle.main.path(forResource:"category", ofType:"csv") else {
102
+
103
+ print("categorycsvファイルがないよ")
104
+
105
+ return
106
+
107
+ }
108
+
109
+
110
+
111
+ do {
112
+
113
+ let csvStringData = try String(contentsOfFile: categoryPath, encoding: String.Encoding.utf8)
114
+
115
+ csvStringData.enumerateLines(invoking: {(line,stop) in
116
+
117
+ let categorySourceArray = line.components(separatedBy:",")
118
+
119
+ let category = Category()
120
+
121
+ category.id = Int(categorySourceArray[0])!
122
+
123
+ category.name = categorySourceArray[1]
124
+
125
+
126
+
127
+ try! realm.write {
128
+
129
+ realm.add(category)
130
+
131
+ }
132
+
133
+ })
134
+
135
+ } catch _ {
136
+
137
+ print("categorycsv読み込みエラー")
138
+
139
+ return
140
+
141
+ }
142
+
143
+
144
+
145
+ //smallcategory.csvの読み込みとrealm化
146
+
147
+ guard let smallcategoryPath = Bundle.main.path(forResource:"smallcategory", ofType:"csv") else {
148
+
149
+ print("smallcategoryファイルがないよ")
150
+
151
+ return
152
+
153
+ }
154
+
155
+
156
+
157
+ do {
158
+
159
+ let csvStringData = try String(contentsOfFile: smallcategoryPath, encoding: String.Encoding.utf8)
160
+
161
+ csvStringData.enumerateLines(invoking: {(line,stop) in
162
+
163
+ let smallcategorySourceArray = line.components(separatedBy:",")
164
+
165
+ let smallcategory = SmallCategory()
166
+
167
+ smallcategory.category = realm.objects(Category.self).filter("id = (smallcategorySourceArray[0])").first
168
+
169
+ smallcategory.id = Int(smallcategorySourceArray[1])!
170
+
171
+ smallcategory.name = smallcategorySourceArray[2]
172
+
173
+ try! realm.write {
174
+
175
+ realm.add(smallcategory)
176
+
177
+ }
178
+
179
+ })
180
+
181
+ } catch _ {
182
+
183
+ print("smallcategory読み込みエラー")
184
+
185
+ return
186
+
187
+ }
188
+
189
+
190
+
191
+ //question.csvの読み込みとrealm化
192
+
193
+ guard let path = Bundle.main.path(forResource:"question", ofType:"csv") else {
194
+
195
+ print("questioncsvファイルがないよ")
196
+
197
+ return
198
+
199
+ }
200
+
201
+
202
+
203
+ do {
204
+
205
+ let csvStringData = try String(contentsOfFile: path, encoding: String.Encoding.utf8)
206
+
207
+ csvStringData.enumerateLines(invoking: {(line,stop) in
208
+
209
+ let questionDataSourceArray = line.components(separatedBy:",")
210
+
211
+ let questionData = Question()
212
+
213
+ try! realm.write {
214
+
215
+ realm.add(questionData)
216
+
217
+ }
218
+
219
+ })
220
+
221
+ } catch _ {
222
+
223
+ print("questioncsv読み込みエラー")
224
+
225
+ return
226
+
227
+ }
228
+
229
+
230
+
231
+ startCount = startCount + 1
232
+
233
+ userDefaults.set(startCount, forKey: "startCount")
234
+
235
+ }
236
+
237
+ }
238
+
239
+ override func viewWillAppear(_ animated: Bool) {
240
+
241
+ super.viewWillAppear(animated)
242
+
243
+ navigationController?.setNavigationBarHidden(true, animated: false)
244
+
245
+ loadView()
246
+
247
+ viewDidLoad()
248
+
249
+
250
+
251
+
252
+
253
+ }
254
+
255
+ }
256
+
257
+
258
+
259
+ ```
260
+
261
+ 問題の画面遷移後のコード
262
+
263
+ ```swift
264
+
265
+ import UIKit
266
+
267
+ import RealmSwift
268
+
269
+
270
+
271
+
272
+
273
+ class TableViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
274
+
275
+
276
+
277
+ @IBOutlet weak var tableView: UITableView!
278
+
279
+ @IBOutlet weak var topSegument: UISegmentedControl!
280
+
281
+
282
+
283
+ // 表示中のカテゴリデータ
284
+
285
+ var category: Category!
286
+
287
+ var questionCount : Int = 0
288
+
289
+ let realm = try! Realm()
290
+
291
+ var point : Int = 0
292
+
293
+ let userDefaults = UserDefaults.standard
294
+
295
+ var countButtonName: UIImage = UIImage(named: "growth0")!
296
+
297
+ var beforeID: Int = 0
298
+
299
+ var questionPoint: Int = 0
300
+
301
+
302
+
303
+
304
+
305
+ override func viewDidLoad() {
306
+
307
+ super .viewDidLoad()
308
+
309
+ userDefaults.register(defaults: ["point": 0])
310
+
311
+ point = readData()
312
+
313
+
314
+
315
+ userDefaults.register(defaults: ["questionPoint": 10])
316
+
317
+ questionPoint = readQuestionPointData()
318
+
319
+ print(questionPoint)
320
+
321
+
322
+
323
+ navigationController?.setNavigationBarHidden(false, animated: false)
324
+
325
+
326
+
327
+ // 初期表示するカテゴリ
328
+
329
+ self.category = realm.objects(Category.self).filter("name == '必要な資質'").first!
330
+
331
+ questionCount = category.questions.count
332
+
333
+ countImageName()
334
+
335
+
336
+
337
+ //navigationbarに設置するアイテム
338
+
339
+ self.tabBarController?.title = "5択問題"
340
+
341
+ let beforeButton = UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action:#selector(beforeQuestion))
342
+
343
+ let countButton = UIBarButtonItem(image: countButtonName.withRenderingMode(.alwaysOriginal), style: UIBarButtonItem.Style.plain, target: self, action:#selector (rewordComment))
344
+
345
+ self.tabBarController?.navigationItem.rightBarButtonItems = [countButton, beforeButton]
346
+
347
+ beforeID = userDefaults.integer(forKey: "BeforeID")
348
+
349
+
350
+
351
+ }
352
+
353
+
354
+
355
+
356
+
357
+ override func viewWillAppear(_ animated: Bool) {
358
+
359
+ super.viewDidDisappear(animated)
360
+
361
+ topSegument.selectedSegmentIndex = 0
362
+
363
+ viewDidLoad()
364
+
365
+ tableView.reloadData()
366
+
367
+ navigationController?.setNavigationBarHidden(false, animated: false)
368
+
369
+
370
+
371
+ }
372
+
373
+
374
+
375
+
376
+
377
+ ```
378
+
379
+ よろしくお願いします。