回答編集履歴
2
修正
test
CHANGED
@@ -15,3 +15,169 @@
|
|
15
15
|
// timeArray => ["00.23", "01.23", "01.55", "03.45", "05.05"]
|
16
16
|
|
17
17
|
```
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
以下回答追記
|
22
|
+
|
23
|
+
---
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
情報を分けた配列で持つのは大変なので、一つの案として`UserDefaults`に保存できるクラスにしてみるのはどうでしょう?
|
28
|
+
|
29
|
+
※ 数が多くなるようであれば`DB`の使用なども考えた方がよいと思いますが。
|
30
|
+
|
31
|
+
古いですが参考URL: [Swiftオブジェクトの配列をNSUserDefaultsに保存する](https://qiita.com/paming/items/a93b8572f65275eb1dd4)
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
```swift
|
36
|
+
|
37
|
+
import UIKit
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
class ViewController: UIViewController {
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
override func viewDidLoad() {
|
46
|
+
|
47
|
+
super.viewDidLoad()
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
let item1 = Item(time: "01.55", date: "2018/05/05", compareTime: 01.55)
|
52
|
+
|
53
|
+
let item2 = Item(time: "01.23", date: "2018/05/04", compareTime: 1.23)
|
54
|
+
|
55
|
+
let item3 = Item(time: "00.23", date: "2018/05/06", compareTime: 0.23)
|
56
|
+
|
57
|
+
let saveItems = [item1, item2, item3]
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
// 保存
|
62
|
+
|
63
|
+
let userDefaults = UserDefaults.standard
|
64
|
+
|
65
|
+
let archivedObject = NSKeyedArchiver.archivedData(withRootObject: saveItems)
|
66
|
+
|
67
|
+
userDefaults.set(archivedObject, forKey: "ItemSaveKey")
|
68
|
+
|
69
|
+
userDefaults.synchronize()
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
// 取得
|
74
|
+
|
75
|
+
var resultItems: [Item] = []
|
76
|
+
|
77
|
+
if let itemData = userDefaults.object(forKey: "ItemSaveKey") as? Data,
|
78
|
+
|
79
|
+
let getItems = NSKeyedUnarchiver.unarchiveObject(with: itemData) as? [Item] {
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
// ソート
|
84
|
+
|
85
|
+
resultItems = getItems.sorted{ $0.time < $1.time }
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
// resultItems => [Item(time: "00.23", date: "2018/05/06", compareTime: 0.23),
|
92
|
+
|
93
|
+
// Item(time: "01.23", date: "2018/05/04", compareTime: 1.23),
|
94
|
+
|
95
|
+
// Item(time: "01.55", date: "2018/05/05", compareTime: 01.55)]
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
class Item: NSObject, NSCoding {
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
enum ItemKey: String {
|
110
|
+
|
111
|
+
case itemTimeKey
|
112
|
+
|
113
|
+
case itemDateKey
|
114
|
+
|
115
|
+
case itemCompareTimeKey
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
/// タイム表示用
|
122
|
+
|
123
|
+
var time: String
|
124
|
+
|
125
|
+
/// 日付
|
126
|
+
|
127
|
+
var date: String
|
128
|
+
|
129
|
+
/// タイム比較用
|
130
|
+
|
131
|
+
var compareTime: Double
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
init(time: String, date: String, compareTime: Double) {
|
136
|
+
|
137
|
+
self.time = time
|
138
|
+
|
139
|
+
self.date = date
|
140
|
+
|
141
|
+
self.compareTime = compareTime
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
required init?(coder aDecoder: NSCoder) {
|
148
|
+
|
149
|
+
guard let time = aDecoder.decodeObject(forKey: Item.ItemKey.itemTimeKey.rawValue) as? String,
|
150
|
+
|
151
|
+
let date = aDecoder.decodeObject(forKey: Item.ItemKey.itemDateKey.rawValue) as? String else {
|
152
|
+
|
153
|
+
return nil
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
self.time = time
|
158
|
+
|
159
|
+
self.date = date
|
160
|
+
|
161
|
+
self.compareTime = aDecoder.decodeDouble(forKey: Item.ItemKey.itemCompareTimeKey.rawValue)
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
func encode(with aCoder: NSCoder) {
|
168
|
+
|
169
|
+
aCoder.encode(time, forKey: Item.ItemKey.itemTimeKey.rawValue)
|
170
|
+
|
171
|
+
aCoder.encode(date, forKey: Item.ItemKey.itemDateKey.rawValue)
|
172
|
+
|
173
|
+
aCoder.encode(compareTime, forKey: Item.ItemKey.itemCompareTimeKey.rawValue)
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
```
|
1
修正
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
文字列
|
1
|
+
文字列(タイム)を配列で保持していると思いますので、配列を以下の用にソートしてみてください。
|
2
2
|
|
3
3
|
|
4
4
|
|