質問編集履歴
1
UserDefaultsの書き方をシンプルにしました。プロパティラッパー等は用いていません。
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,39 +8,43 @@
|
|
8
8
|
|
9
9
|
##試みている解決方法
|
10
10
|
|
11
|
+
UserDefaultsによって永続化を試みています。
|
12
|
+
|
13
|
+
```
|
14
|
+
|
11
|
-
|
15
|
+
UserDefaults.standard.set(self.favoriteArray.rgbArray,forKey: "stored")
|
12
|
-
|
13
|
-
|
14
|
-
|
16
|
+
|
15
|
-
|
17
|
+
self.favoriteArray.rgbArray.append([self.r,self.g,self.b])
|
16
|
-
|
18
|
+
|
17
|
-
`
|
19
|
+
```
|
18
|
-
|
19
|
-
|
20
|
-
|
20
|
+
|
21
|
-
|
21
|
+
によってrgbArrayにUseDefaultsを用いています。
|
22
|
+
|
22
|
-
|
23
|
+
リストを表示するビューでは
|
24
|
+
|
23
|
-
|
25
|
+
```
|
24
|
-
|
25
|
-
|
26
|
-
|
26
|
+
|
27
|
-
|
27
|
+
let userdefaultRgbArray: [[Int]] = UserDefaults.standard.array(forKey: "stored") as! [[Int]]
|
28
|
+
|
28
|
-
|
29
|
+
var body: some View {
|
30
|
+
|
31
|
+
|
32
|
+
|
29
|
-
|
33
|
+
List {
|
30
|
-
|
34
|
+
|
31
|
-
ForEach(0..<self.s
|
35
|
+
ForEach(0..<self.userdefaultRgbArray.count) { item in
|
32
|
-
|
36
|
+
|
37
|
+
|
38
|
+
|
33
|
-
ListRow(rValue: self.s
|
39
|
+
ListRow(rValue: self.userdefaultRgbArray[item][0], gValue: self.userdefaultRgbArray[item][1], bValue: self.userdefaultRgbArray[item][2])
|
34
|
-
|
40
|
+
|
35
|
-
}
|
41
|
+
}
|
42
|
+
|
36
|
-
|
43
|
+
}
|
44
|
+
|
37
|
-
```
|
45
|
+
```
|
38
|
-
|
46
|
+
|
39
|
-
のように配列の数だけリスト
|
47
|
+
のようにして配列の要素の数だけリストが増えるようになっています。
|
40
|
-
|
41
|
-
ここで`ListRow`は3つのInt型を引数に持ちそれを背景色にもつビューです。
|
42
|
-
|
43
|
-
|
44
48
|
|
45
49
|
##起きているエラー
|
46
50
|
|
@@ -66,6 +70,8 @@
|
|
66
70
|
|
67
71
|
|
68
72
|
|
73
|
+
|
74
|
+
|
69
75
|
struct ColorDetail: View {
|
70
76
|
|
71
77
|
var r:Int
|
@@ -76,9 +82,15 @@
|
|
76
82
|
|
77
83
|
@EnvironmentObject var favoriteArray: ObservedRGB
|
78
84
|
|
85
|
+
|
86
|
+
|
79
|
-
|
87
|
+
var userDefaults = UserDefaults.standard
|
80
|
-
|
88
|
+
|
89
|
+
|
90
|
+
|
81
|
-
var body: some View {
|
91
|
+
var body: some View {
|
92
|
+
|
93
|
+
|
82
94
|
|
83
95
|
GeometryReader{ bodyView in
|
84
96
|
|
@@ -96,174 +108,186 @@
|
|
96
108
|
|
97
109
|
print("tapped")
|
98
110
|
|
111
|
+
UserDefaults.standard.set(self.favoriteArray.rgbArray,forKey: "stored") //rgbArray永続的に保存する
|
112
|
+
|
113
|
+
self.favoriteArray.rgbArray.append([self.r,self.g,self.b])
|
114
|
+
|
115
|
+
}){
|
116
|
+
|
117
|
+
Text("保存する")
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
extension UIColor {
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
convenience init(_ red: Int, _ green: Int, _ blue: Int, _ alpha: Int = 255) {
|
138
|
+
|
139
|
+
let rgba = [red, green, blue, alpha].map { i -> CGFloat in
|
140
|
+
|
141
|
+
switch i {
|
142
|
+
|
143
|
+
case let i where i < 0:
|
144
|
+
|
145
|
+
return 0
|
146
|
+
|
147
|
+
case let i where i > 255:
|
148
|
+
|
149
|
+
return 1
|
150
|
+
|
151
|
+
default:
|
152
|
+
|
153
|
+
return CGFloat(i) / 255
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
self.init(red: rgba[0], green: rgba[1], blue: rgba[2], alpha: rgba[3])
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
struct ColorDetail_Previews: PreviewProvider {
|
168
|
+
|
169
|
+
static let favoriteArray = ObservedRGB()
|
170
|
+
|
171
|
+
static var previews: some View {
|
172
|
+
|
173
|
+
ColorDetail(r: 3,g:12,b:21).environmentObject(favoriteArray)
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
```
|
180
|
+
|
181
|
+
リストに並べられるビュー
|
182
|
+
|
183
|
+
```ListRow
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
import SwiftUI
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
struct ListRow: View {
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
var rValue: Int
|
196
|
+
|
197
|
+
var gValue: Int
|
198
|
+
|
199
|
+
var bValue: Int
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
var body: some View {
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
Text("(String(rValue, radix: 16))(String(gValue, radix: 16))(String(bValue, radix: 16))")
|
212
|
+
|
213
|
+
.frame(width: 300, height: 50)
|
214
|
+
|
215
|
+
.background(Color.init(UIColor(rValue,gValue,bValue)))
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
struct ListRow_Previews: PreviewProvider {
|
226
|
+
|
227
|
+
static let favoriteArray = ObservedRGB()
|
228
|
+
|
229
|
+
static var previews: some View {
|
230
|
+
|
231
|
+
ListRow(rValue: 1, gValue: 0, bValue: 0)
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
```
|
238
|
+
|
239
|
+
リストのビュー
|
240
|
+
|
241
|
+
```MainList
|
242
|
+
|
243
|
+
import SwiftUI
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
struct MainList: View {
|
248
|
+
|
249
|
+
@EnvironmentObject var favoriteArray: ObservedRGB
|
250
|
+
|
251
|
+
let userdefaultRgbArray: [[Int]] = UserDefaults.standard.array(forKey: "stored") as! [[Int]]
|
252
|
+
|
253
|
+
var body: some View {
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
List {
|
258
|
+
|
259
|
+
ForEach(0..<self.userdefaultRgbArray.count) { item in
|
260
|
+
|
99
261
|
|
100
262
|
|
101
|
-
//self.favoriteArray.rgbArray.append([self.r,self.g,self.b])
|
102
|
-
|
103
|
-
self.userdefal
|
263
|
+
ListRow(rValue: self.userdefaultRgbArray[item][0], gValue: self.userdefaultRgbArray[item][1], bValue: self.userdefaultRgbArray[item][2])
|
104
|
-
|
105
|
-
|
264
|
+
|
106
|
-
|
107
|
-
Text("保存する")
|
108
|
-
|
109
|
-
}
|
265
|
+
}
|
110
|
-
|
266
|
+
|
111
|
-
}
|
267
|
+
}
|
112
|
-
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
|
278
|
+
|
113
|
-
|
279
|
+
}
|
114
|
-
|
280
|
+
|
115
|
-
|
281
|
+
}
|
116
|
-
|
117
|
-
|
282
|
+
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
283
|
+
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
284
|
+
|
128
|
-
|
129
|
-
let rgba = [red, green, blue, alpha].map { i -> CGFloat in
|
130
|
-
|
131
|
-
switch i {
|
132
|
-
|
133
|
-
case let i where i < 0:
|
134
|
-
|
135
|
-
return 0
|
136
|
-
|
137
|
-
case let i where i > 255:
|
138
|
-
|
139
|
-
return 1
|
140
|
-
|
141
|
-
default:
|
142
|
-
|
143
|
-
return CGFloat(i) / 255
|
144
|
-
|
145
|
-
}
|
146
|
-
|
147
|
-
}
|
148
|
-
|
149
|
-
self.init(red: rgba[0], green: rgba[1], blue: rgba[2], alpha: rgba[3])
|
150
|
-
|
151
|
-
}
|
152
|
-
|
153
|
-
}
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
struct
|
285
|
+
struct MainList_Previews: PreviewProvider {
|
158
286
|
|
159
287
|
static let favoriteArray = ObservedRGB()
|
160
288
|
|
161
289
|
static var previews: some View {
|
162
290
|
|
163
|
-
ColorDetail(r: 3,g:12,b:21).environmentObject(favoriteArray)
|
164
|
-
|
165
|
-
}
|
166
|
-
|
167
|
-
}
|
168
|
-
|
169
|
-
```
|
170
|
-
|
171
|
-
リストに並べられるビュー
|
172
|
-
|
173
|
-
```ListRow
|
174
|
-
|
175
|
-
mport SwiftUI
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
struct ListRow: View {
|
180
|
-
|
181
|
-
@EnvironmentObject var favoriteArray: ObservedRGB
|
182
|
-
|
183
|
-
var rValue: Int
|
184
|
-
|
185
|
-
var gValue: Int
|
186
|
-
|
187
|
-
var bValue: Int
|
188
|
-
|
189
|
-
var storedArray = UserDefaults.standard.array(forKey: "udrgbArray")
|
190
|
-
|
191
|
-
var body: some View {
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
Text("(String(rValue, radix: 16))(String(gValue, radix: 16))(String(bValue, radix: 16))")
|
196
|
-
|
197
|
-
.frame(width: 300, height: 50)
|
198
|
-
|
199
|
-
.background(Color.init(UIColor(rValue,gValue,bValue)))
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
}
|
204
|
-
|
205
|
-
}
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
struct ListRow_Previews: PreviewProvider {
|
210
|
-
|
211
|
-
static let favoriteArray = ObservedRGB()
|
212
|
-
|
213
|
-
static var previews: some View {
|
214
|
-
|
215
|
-
ListRow(rValue: 0, gValue: 0, bValue: 0).environmentObject(favoriteArray)
|
216
|
-
|
217
|
-
}
|
218
|
-
|
219
|
-
}
|
220
|
-
|
221
|
-
```
|
222
|
-
|
223
|
-
リストのビュー
|
224
|
-
|
225
|
-
```MainList
|
226
|
-
|
227
|
-
import SwiftUI
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
struct MainList: View {
|
232
|
-
|
233
|
-
@EnvironmentObject var favoriteArray: ObservedRGB
|
234
|
-
|
235
|
-
var storedArray:[[Int]] = UserDefaults.standard.array(forKey: "udrgbArray") as! [[Int]]
|
236
|
-
|
237
|
-
var body: some View {
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
List {
|
242
|
-
|
243
|
-
ForEach(0..<self.storedArray.count) { item in
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
ListRow(rValue: self.storedArray[item][0], gValue: self.storedArray[item][1], bValue: self.storedArray[item][2])
|
248
|
-
|
249
|
-
}
|
250
|
-
|
251
|
-
}
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
}
|
256
|
-
|
257
|
-
}
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
struct MainList_Previews: PreviewProvider {
|
262
|
-
|
263
|
-
static let favoriteArray = ObservedRGB()
|
264
|
-
|
265
|
-
static var previews: some View {
|
266
|
-
|
267
291
|
MainList().environmentObject(favoriteArray)
|
268
292
|
|
269
293
|
}
|