質問編集履歴
2
ViewController(initialViewController)とNextViewcontrollerのコードを両方記載しました。また、実現したいことの項目等にも追記しています。
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,33 +2,45 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
構造体
|
5
|
+
NextViewControllerで構造体をUserDefaults.standardに保存したのですが、保存したものをViewControllerで利用したいです。
|
6
|
-
|
7
|
-
|
6
|
+
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
|
8
|
+
|
11
|
-
構造体に
|
9
|
+
構造体にtodo(Stringa型)とrate(Int型)を保存しており、
|
10
|
+
|
12
|
-
|
11
|
+
これをViewControllerへ渡して、TableViewのCellで利用したいです。
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
TODOリストにレーティング機能がついたようなアプリを作成しており、
|
16
|
+
|
13
|
-
rate
|
17
|
+
rate == 1のときに、入力したテキスト(todo)をセクション1(heart1)へ反映させる、
|
14
|
-
|
15
|
-
|
16
|
-
|
18
|
+
|
17
|
-
|
19
|
+
rate == 1のときに、入力したテキスト(todo)をセクション1(heart2)へ反映させる、といった形で動かしたいと考えております。
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
|
18
|
-
|
25
|
+
### やったこと
|
26
|
+
|
19
|
-
|
27
|
+
ViewControllerで利用しようとすると、エラーが出てしまいます。
|
28
|
+
|
29
|
+
|
30
|
+
|
20
|
-
|
31
|
+
let heart1 = UserDefaults.standard.data(forKey:"DoneStructList")!.filter {$0.rate==1
|
32
|
+
|
21
|
-
|
33
|
+
}
|
34
|
+
|
22
|
-
|
35
|
+
**→ これを入れるとエラーが出てしまいます。**
|
23
|
-
|
24
|
-
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
|
25
|
-
###
|
41
|
+
### エラーコード
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
42
|
+
|
30
|
-
|
31
|
-
Value of type '
|
43
|
+
Value of type 'Data.Element' (aka 'UInt8') has no member 'rate'
|
32
44
|
|
33
45
|
|
34
46
|
|
@@ -36,10 +48,380 @@
|
|
36
48
|
|
37
49
|
```swift
|
38
50
|
|
51
|
+
|
52
|
+
|
53
|
+
struct DoneStructList: Codable {
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
var list = [DoneStruct]()
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
struct DoneStruct: Codable{
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
var todo = String()
|
66
|
+
|
67
|
+
var rate = Int()
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
class NextViewController: UIViewController {
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
private var list: [DoneStructList.DoneStruct] = []
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
var rate = 0
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
@IBOutlet weak var TextField: UITextField!
|
94
|
+
|
95
|
+
@IBOutlet weak var Label: UILabel!
|
96
|
+
|
97
|
+
@IBOutlet weak var heart1: UIButton!
|
98
|
+
|
99
|
+
@IBOutlet weak var heart2: UIButton!
|
100
|
+
|
101
|
+
@IBOutlet weak var heart3: UIButton!
|
102
|
+
|
103
|
+
@IBOutlet weak var heart4: UIButton!
|
104
|
+
|
105
|
+
@IBOutlet weak var heart5: UIButton!
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
let heartFill = UIImage(named: "heart")
|
110
|
+
|
111
|
+
let heartEmpty = UIImage(named: "emptyheart")
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
override func viewDidLoad() {
|
116
|
+
|
117
|
+
super.viewDidLoad()
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
list = getValue() ?? []
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
@IBAction func AddButton(_ sender: Any) {
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
//配列listの中にある構造体todo,rateそれぞれにTextField.textとrateを保存する。
|
136
|
+
|
137
|
+
let doneStruct = DoneStructList.DoneStruct(todo: TextField.text!,rate: rate)
|
138
|
+
|
139
|
+
list.append(doneStruct)
|
140
|
+
|
141
|
+
save(list: list)
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
dismiss(animated: true, completion: nil)
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
//構造体を UserDefaultsへ保存するためにデータ型にエンコード
|
152
|
+
|
153
|
+
private func serialize(doneStructList:DoneStructList) -> Data?{
|
154
|
+
|
155
|
+
do{
|
156
|
+
|
157
|
+
let encoder = JSONEncoder()
|
158
|
+
|
159
|
+
return try
|
160
|
+
|
161
|
+
encoder.encode(doneStructList)
|
162
|
+
|
163
|
+
}catch {
|
164
|
+
|
165
|
+
return nil
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
//データ型からデコード
|
174
|
+
|
175
|
+
private func processJson(data: Data) -> [DoneStructList.DoneStruct]? {
|
176
|
+
|
177
|
+
do {
|
178
|
+
|
179
|
+
let jsonDecoder = JSONDecoder()
|
180
|
+
|
181
|
+
let doneStructList = try
|
182
|
+
|
183
|
+
jsonDecoder.decode(DoneStructList.self,from: data)
|
184
|
+
|
185
|
+
return doneStructList.list
|
186
|
+
|
187
|
+
}catch {
|
188
|
+
|
189
|
+
return nil
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
//UserDefaultsに保存
|
198
|
+
|
199
|
+
private func save(list: [DoneStructList.DoneStruct]){
|
200
|
+
|
201
|
+
let doneStructList = DoneStructList(list: list)
|
202
|
+
|
203
|
+
guard let data = serialize(doneStructList: doneStructList)else{
|
204
|
+
|
205
|
+
return
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
UserDefaults.standard.setValue(data, forKeyPath: "DoneStructList")
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
//UserDefaultsから取得
|
216
|
+
|
217
|
+
private func getValue() -> [DoneStructList.DoneStruct]? {
|
218
|
+
|
219
|
+
guard let data = UserDefaults.standard.data(forKey: "DoneStructList")else
|
220
|
+
|
221
|
+
{return nil}
|
222
|
+
|
223
|
+
return processJson(data: data)
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
//以下ハートをタップしたら色が変わる、ラベルに数字が反映される。
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
@IBAction func heart1Tapped(_ sender: Any) {
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
switch rate {
|
238
|
+
|
239
|
+
case 1:
|
240
|
+
|
241
|
+
rate = 0
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
heart1.setImage(heartEmpty, for:UIControl.State() )
|
246
|
+
|
247
|
+
heart2.setImage(heartEmpty, for:UIControl.State() )
|
248
|
+
|
249
|
+
heart3.setImage(heartEmpty, for:UIControl.State() )
|
250
|
+
|
251
|
+
heart4.setImage(heartEmpty, for:UIControl.State() )
|
252
|
+
|
253
|
+
heart5.setImage(heartEmpty, for:UIControl.State() )
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
default:
|
260
|
+
|
261
|
+
rate = 1
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
heart1.setImage(heartFill, for:UIControl.State() )
|
266
|
+
|
267
|
+
heart2.setImage(heartEmpty, for:UIControl.State() )
|
268
|
+
|
269
|
+
heart3.setImage(heartEmpty, for:UIControl.State() )
|
270
|
+
|
271
|
+
heart4.setImage(heartEmpty, for:UIControl.State() )
|
272
|
+
|
273
|
+
heart5.setImage(heartEmpty, for:UIControl.State() )
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
Label.text = String(rate)
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
@IBAction func heart2Tapped(_ sender: Any) {
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
//ボタンをタップした時の動作
|
292
|
+
|
293
|
+
rate = 2
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
heart1.setImage(heartFill, for:UIControl.State() )
|
298
|
+
|
299
|
+
heart2.setImage(heartFill, for:UIControl.State() )
|
300
|
+
|
301
|
+
heart3.setImage(heartEmpty, for:UIControl.State() )
|
302
|
+
|
303
|
+
heart4.setImage(heartEmpty, for:UIControl.State() )
|
304
|
+
|
305
|
+
heart5.setImage(heartEmpty, for:UIControl.State() )
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
Label.text = String(rate)
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
@IBAction func heart3Tapped(_ sender: Any) {
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
rate = 3
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
heart1.setImage(heartFill, for:UIControl.State() )
|
326
|
+
|
327
|
+
heart2.setImage(heartFill, for:UIControl.State() )
|
328
|
+
|
329
|
+
heart3.setImage(heartFill, for:UIControl.State() )
|
330
|
+
|
331
|
+
heart4.setImage(heartEmpty, for:UIControl.State() )
|
332
|
+
|
333
|
+
heart5.setImage(heartEmpty, for:UIControl.State() )
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
Label.text = String(rate)
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
}
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
@IBAction func heart4Tapped(_ sender: Any) {
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
rate = 4
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
heart1.setImage(heartFill, for:UIControl.State() )
|
354
|
+
|
355
|
+
heart2.setImage(heartFill, for:UIControl.State() )
|
356
|
+
|
357
|
+
heart3.setImage(heartFill, for:UIControl.State() )
|
358
|
+
|
359
|
+
heart4.setImage(heartFill, for:UIControl.State() )
|
360
|
+
|
361
|
+
heart5.setImage(heartEmpty, for:UIControl.State() )
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
Label.text = String(rate)
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
}
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
@IBAction func heart5Tapped(_ sender: Any) {
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
rate = 5
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
heart1.setImage(heartFill, for:UIControl.State() )
|
384
|
+
|
385
|
+
heart2.setImage(heartFill, for:UIControl.State() )
|
386
|
+
|
387
|
+
heart3.setImage(heartFill, for:UIControl.State() )
|
388
|
+
|
389
|
+
heart4.setImage(heartFill, for:UIControl.State() )
|
390
|
+
|
391
|
+
heart5.setImage(heartFill, for:UIControl.State() )
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
Label.text = String(rate)
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
}
|
400
|
+
|
401
|
+
```
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
```
|
412
|
+
|
413
|
+
|
414
|
+
|
415
|
+
//NextViewControllerで保存した値を以下のViewControllerで使用したい。
|
416
|
+
|
417
|
+
|
418
|
+
|
39
419
|
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
|
40
420
|
|
41
421
|
|
42
422
|
|
423
|
+
//いったん文字を入れておく。ここにNextViewControllerで保存したtodoを反映させたい。rate==1のときに入力したテキスト(todo)をheart1へ入れる、rate==2のときに入力したテキスト(todo)をheart2へ入れるという形でいれたい。
|
424
|
+
|
43
425
|
let heart1 = ["ご飯を食べる"]
|
44
426
|
|
45
427
|
let heart2 = ["夜更かし","ご飯を食べる"]
|
@@ -50,13 +432,19 @@
|
|
50
432
|
|
51
433
|
let heart5 = ["温泉に行く","服を買いに行く"]
|
52
434
|
|
435
|
+
|
436
|
+
|
53
437
|
let sectionTitles = ["♡1","♡2","♡3","♡4","♡5"]
|
54
438
|
|
55
439
|
|
56
440
|
|
441
|
+
|
442
|
+
|
57
443
|
@IBOutlet weak var tableView: UITableView!
|
58
444
|
|
59
|
-
|
445
|
+
|
446
|
+
|
447
|
+
|
60
448
|
|
61
449
|
override func viewDidLoad() {
|
62
450
|
|
@@ -66,226 +454,174 @@
|
|
66
454
|
|
67
455
|
|
68
456
|
|
457
|
+
|
458
|
+
|
69
459
|
tableView.delegate = self
|
70
460
|
|
71
461
|
tableView.dataSource = self
|
72
462
|
|
73
463
|
}
|
74
464
|
|
75
|
-
|
76
|
-
|
465
|
+
|
466
|
+
|
77
|
-
//
|
467
|
+
//5つのセクションを用意する
|
468
|
+
|
78
|
-
|
469
|
+
func numberOfSections(in tableView: UITableView) -> Int {
|
470
|
+
|
471
|
+
return 5
|
472
|
+
|
473
|
+
}
|
474
|
+
|
475
|
+
|
476
|
+
|
79
|
-
|
477
|
+
//sectionTitlesの数だけセクションを用意する
|
478
|
+
|
80
|
-
|
479
|
+
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
81
|
-
|
82
|
-
|
480
|
+
|
83
|
-
|
481
|
+
return sectionTitles[section]
|
482
|
+
|
84
|
-
|
483
|
+
}
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
488
|
+
|
489
|
+
|
490
|
+
|
85
|
-
s
|
491
|
+
if section == 0{
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
492
|
+
|
90
|
-
|
91
|
-
|
493
|
+
return heart1.count
|
494
|
+
|
495
|
+
}else if section == 1{
|
496
|
+
|
497
|
+
return heart2.count
|
498
|
+
|
499
|
+
}else if section == 2{
|
500
|
+
|
501
|
+
return heart3.count
|
502
|
+
|
503
|
+
}else if section == 3{
|
504
|
+
|
505
|
+
return heart4.count
|
506
|
+
|
507
|
+
}else if section == 4{
|
508
|
+
|
509
|
+
return heart5.count
|
510
|
+
|
511
|
+
}else{
|
512
|
+
|
513
|
+
return 0
|
514
|
+
|
515
|
+
}
|
516
|
+
|
517
|
+
}
|
518
|
+
|
519
|
+
|
520
|
+
|
521
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
522
|
+
|
523
|
+
|
524
|
+
|
525
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",for: indexPath)
|
526
|
+
|
527
|
+
|
528
|
+
|
529
|
+
|
530
|
+
|
531
|
+
if indexPath.section == 0 {
|
532
|
+
|
533
|
+
cell.textLabel?.text =heart1[indexPath.row]
|
534
|
+
|
535
|
+
|
536
|
+
|
537
|
+
}else if indexPath.section == 1{
|
538
|
+
|
539
|
+
cell.textLabel?.text = heart2[indexPath.row]
|
540
|
+
|
541
|
+
|
542
|
+
|
543
|
+
}else if indexPath.section == 2{
|
544
|
+
|
545
|
+
cell.textLabel?.text = heart3[indexPath.row]
|
546
|
+
|
547
|
+
|
548
|
+
|
549
|
+
}else if indexPath.section == 3{
|
550
|
+
|
551
|
+
cell.textLabel?.text = heart4[indexPath.row]
|
552
|
+
|
553
|
+
|
554
|
+
|
555
|
+
}else if indexPath.section == 4{
|
556
|
+
|
557
|
+
cell.textLabel?.text = heart5[indexPath.row]
|
92
558
|
|
93
559
|
}
|
94
560
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
return nil
|
120
|
-
|
121
|
-
}
|
122
|
-
|
123
|
-
}
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
//データ型からデコード
|
128
|
-
|
129
|
-
private func processJson(data: Data) -> [DoneStructList.DoneStruct]?{
|
130
|
-
|
131
|
-
do{
|
132
|
-
|
133
|
-
let jsonDecoder = JSONDecoder()
|
134
|
-
|
135
|
-
let doneStructList = try
|
136
|
-
|
137
|
-
jsonDecoder.decode(DoneStructList.self, from: data)
|
138
|
-
|
139
|
-
return doneStructList.list
|
140
|
-
|
141
|
-
}catch{
|
142
|
-
|
143
|
-
return nil
|
144
|
-
|
145
|
-
}
|
146
|
-
|
147
|
-
}
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
//UserDefaultsに保存
|
152
|
-
|
153
|
-
private func save(){
|
154
|
-
|
155
|
-
guard let data = serialize() else
|
156
|
-
|
157
|
-
{ return }
|
158
|
-
|
159
|
-
UserDefaults.standard.setValue(data, forKeyPath: "DoneStructList")
|
160
|
-
|
161
|
-
}
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
//UserDefaultsから取得
|
166
|
-
|
167
|
-
func getValue() -> [DoneStructList.DoneStruct]?{
|
168
|
-
|
169
|
-
guard let data = UserDefaults.standard.data(forKey: "DoneStructList") else
|
170
|
-
|
171
|
-
{ return nil }
|
172
|
-
|
173
|
-
return processJson(data: data)
|
174
|
-
|
175
|
-
}
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
if section == 0{
|
184
|
-
|
185
|
-
return heart1.count
|
186
|
-
|
187
|
-
}else if section == 1{
|
188
|
-
|
189
|
-
return heart2.count
|
190
|
-
|
191
|
-
}else if section == 2{
|
192
|
-
|
193
|
-
return heart3.count
|
194
|
-
|
195
|
-
}else if section == 3{
|
196
|
-
|
197
|
-
return heart4.count
|
198
|
-
|
199
|
-
}else if section == 4{
|
200
|
-
|
201
|
-
return heart5.count
|
202
|
-
|
203
|
-
}else{
|
204
|
-
|
205
|
-
return 0
|
206
|
-
|
207
|
-
}
|
208
|
-
|
209
|
-
}
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
if indexPath.section == 0 {
|
222
|
-
|
223
|
-
cell.textLabel?.text = heart1[indexPath.row]
|
224
|
-
|
225
|
-
}else if indexPath.section == 1{
|
226
|
-
|
227
|
-
cell.textLabel?.text = heart2[indexPath.row]
|
228
|
-
|
229
|
-
}else if indexPath.section == 2{
|
230
|
-
|
231
|
-
cell.textLabel?.text = heart3[indexPath.row]
|
232
|
-
|
233
|
-
}else if indexPath.section == 3{
|
234
|
-
|
235
|
-
cell.textLabel?.text = heart4[indexPath.row]
|
236
|
-
|
237
|
-
}else if indexPath.section == 4{
|
238
|
-
|
239
|
-
cell.textLabel?.text = heart5[indexPath.row]
|
240
|
-
|
241
|
-
}
|
242
|
-
|
243
|
-
return cell
|
244
|
-
|
245
|
-
}
|
561
|
+
|
562
|
+
|
563
|
+
return cell
|
564
|
+
|
565
|
+
|
566
|
+
|
567
|
+
}
|
568
|
+
|
569
|
+
|
570
|
+
|
571
|
+
@IBAction func AddButton(_ sender: Any) {
|
572
|
+
|
573
|
+
|
574
|
+
|
575
|
+
performSegue(withIdentifier: "next", sender: nil)
|
576
|
+
|
577
|
+
|
578
|
+
|
579
|
+
}
|
580
|
+
|
581
|
+
|
582
|
+
|
583
|
+
}
|
246
584
|
|
247
585
|
```
|
248
586
|
|
249
587
|
|
250
588
|
|
251
|
-
|
252
|
-
|
253
589
|
|
254
590
|
|
255
591
|
|
256
592
|
|
257
593
|
### 試したこと
|
258
594
|
|
595
|
+
|
596
|
+
|
259
|
-
let heart1 =
|
597
|
+
let heart1 = UserDefaults.standard.data(forKey:"DoneStructList")!.filter {$0.rate==1
|
598
|
+
|
260
|
-
|
599
|
+
}
|
600
|
+
|
601
|
+
**→ これを入れるとエラーが出てしまいます。**
|
602
|
+
|
261
|
-
**→rate
|
603
|
+
**→rate==1のときに入力したテキスト(todo)をheart1へ入れたい**
|
262
604
|
|
263
605
|
let heart2 = ["夜更かし","ご飯を食べる"]
|
264
606
|
|
265
|
-
**→rate
|
607
|
+
**→rate==2のときに入力したテキスト(todo)をheart2へ入れたい**
|
266
608
|
|
267
609
|
let heart3 = ["片付けする","ご飯を食べる"]
|
268
610
|
|
269
|
-
** →rate
|
611
|
+
** →rate==3のときに入力したテキスト(todo)をheart3へ入れたい**
|
270
612
|
|
271
613
|
let heart4 = ["タバコを吸う","散歩する","サウナに行く","家で半身浴"]
|
272
614
|
|
273
|
-
**→rate
|
615
|
+
**→rrate==4のときに入力したテキスト(todo)をheart4へ入れたい**
|
274
616
|
|
275
617
|
let heart5 = ["温泉に行く","服を買いに行く"]
|
276
618
|
|
277
|
-
**→rate
|
619
|
+
**→rate==5のときに入力したテキスト(todo)をheart5へ入れたい**
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
620
|
+
|
621
|
+
|
622
|
+
|
623
|
+
|
624
|
+
|
283
|
-
|
625
|
+
NextViewControllerで保存したStructをViewControllerで使いたいのですが、
|
284
|
-
|
626
|
+
|
285
|
-
いいでしょうか。
|
627
|
+
どのように実装すればいいでしょうか。
|
286
|
-
|
287
|
-
rateLabel==1の時に、heart1へtodonakamiを,
|
288
|
-
|
289
|
-
rateLabel==2の時に、heart2へtodonakamiを,
|
290
|
-
|
291
|
-
というような形で入れたいです。
|
1
UserDefaultsへ保存するコードを記載
test
CHANGED
File without changes
|
test
CHANGED
@@ -96,6 +96,34 @@
|
|
96
96
|
|
97
97
|
|
98
98
|
|
99
|
+
var doneStructList = DoneStructList()
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
//データ型にエンコード
|
104
|
+
|
105
|
+
private func serialize() -> Data?{
|
106
|
+
|
107
|
+
do{
|
108
|
+
|
109
|
+
let encoder = JSONEncoder()
|
110
|
+
|
111
|
+
let data = try
|
112
|
+
|
113
|
+
encoder.encode(doneStructList)
|
114
|
+
|
115
|
+
return data
|
116
|
+
|
117
|
+
}catch{
|
118
|
+
|
119
|
+
return nil
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
|
99
127
|
//データ型からデコード
|
100
128
|
|
101
129
|
private func processJson(data: Data) -> [DoneStructList.DoneStruct]?{
|
@@ -118,6 +146,20 @@
|
|
118
146
|
|
119
147
|
}
|
120
148
|
|
149
|
+
|
150
|
+
|
151
|
+
//UserDefaultsに保存
|
152
|
+
|
153
|
+
private func save(){
|
154
|
+
|
155
|
+
guard let data = serialize() else
|
156
|
+
|
157
|
+
{ return }
|
158
|
+
|
159
|
+
UserDefaults.standard.setValue(data, forKeyPath: "DoneStructList")
|
160
|
+
|
161
|
+
}
|
162
|
+
|
121
163
|
|
122
164
|
|
123
165
|
//UserDefaultsから取得
|