回答編集履歴
1
サンプルソース追加
test
CHANGED
@@ -15,3 +15,43 @@
|
|
15
15
|
|
16
16
|
|
17
17
|
あとは変換した辞書を配列に入れてそれをUserDefaultに格納すれば行けると思います。
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
----
|
22
|
+
|
23
|
+
追記、サンプルソースを載せておきます。
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
```swift:
|
28
|
+
|
29
|
+
let list: [(title: String, time: String)] = [("AA", "BB"), ("CC", "DD")]
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
// タプルの配列を辞書の配列へ変換
|
34
|
+
|
35
|
+
let saveData: [[String: Any]] = list.map { ["title": $0.title, "time": $0.time] }
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
UserDefaults.standard.set(saveData, forKey: "list")
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
if let loadData = UserDefaults.standard.object(forKey: "list") as? [[String: Any]] {
|
44
|
+
|
45
|
+
// 辞書の配列をタプルの配列へ変換(エラー処理が乱暴ですが)
|
46
|
+
|
47
|
+
let list2 = loadData.map { (title: $0["title"] as! String, time: $0["time"] as! String) }
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
print(list) // [(title: "AA", time: "BB"), (title: "CC", time: "DD")]
|
52
|
+
|
53
|
+
print(list2) // [(title: "AA", time: "BB"), (title: "CC", time: "DD")]
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
```
|