質問編集履歴

11

編集

2023/04/03 09:36

投稿

sato31
sato31

スコア8

test CHANGED
File without changes
test CHANGED
@@ -34,9 +34,9 @@
34
34
  private var items: FetchedResults<Item>
35
35
 
36
36
  private let list1 = [
37
- SampleStruct(sectionTitle: "A", listContent: ["elementA1"],systemNameText: "pin", folderRowName: "a"),
37
+ SampleStruct(sectionTitle: "A", listContent: ["elementA1", "elementA2", "elementA3"]),
38
- SampleStruct(sectionTitle: "B", listContent: ["elementB1"],systemNameText: "pin", folderRowName: "a"),
38
+ SampleStruct(sectionTitle: "B", listContent: ["elementB1", "elementB2", "elementB3"]),
39
- SampleStruct(sectionTitle: "C", listContent: ["elementC1"],systemNameText: "pin", folderRowName: "a"),
39
+ SampleStruct(sectionTitle: "C", listContent: ["elementC1", "elementC2", "elementC3"]),
40
40
  ]
41
41
 
42
42
  var body: some View {
@@ -44,9 +44,8 @@
44
44
  List {
45
45
  ForEach(list1) { item in
46
46
  Section(item.sectionTitle) {
47
- ForEach(item.listContent, id: \.self) { item2 in NavigationLink{ArrayView()}label: {
47
+ ForEach(item.listContent, id: \.self) { item2 in
48
- SampleStruct(sectionTitle: "A", listContent: ["elementA1"],systemNameText: "pin", folderRowName: "a")}
49
- }
48
+ Text(item2)}
50
49
  .toolbar {
51
50
  ToolbarItem(placement: .navigationBarTrailing) {
52
51
  EditButton()
@@ -56,157 +55,28 @@
56
55
  }
57
56
  }
58
57
  .navigationTitle("A")
58
+ }}}
59
+
60
+ struct FolderRow: View {
61
+ var folderRowArray: [String]
62
+ var systemNameText: String
63
+ var folderRowName: String
64
+ var body: some View {
65
+ HStack{
66
+ Image(systemName: systemNameText)
67
+ Text(folderRowName)
68
+ Text("\(folderRowArray.count)")
59
- }
69
+ }
60
- }
70
+ }
61
71
  }
62
72
 
63
- struct SampleStruct : View, Identifiable {
73
+ struct SampleStruct : Identifiable {
64
- let id = UUID()
74
+ let id = UUID()
65
- let sectionTitle: String
75
+ let sectionTitle: String
66
- let listContent: [String]
76
+ let listContent: [String]
67
- let systemNameText: String
68
- let folderRowName: String
69
- var body: some View {
70
- HStack{
71
- Image(systemName: systemNameText)
72
- Text(folderRowName)
73
- Text("\(systemNameText.count)")
74
- }
75
- }
76
- }
77
-
78
- struct ArrayView: View {
79
- @Environment(\.managedObjectContext) private var viewContext
80
-
81
- @FetchRequest(
82
- sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: false)],
83
- animation: .default)
84
- private var items: FetchedResults<Item>
85
-
86
- var body: some View {
87
- NavigationView {
88
- List {
89
- ForEach(items) { item in
90
- NavigationLink {
91
- AnotherView(item: item)
92
- } label: {
93
- Text(item.text ?? "")
94
- }
95
77
  }
96
- .onDelete(perform: deleteItems)
97
- }
98
- .toolbar {
99
- #if os(iOS)
100
- ToolbarItem(placement: .navigationBarTrailing) {
101
- EditButton()
102
- }
103
- #endif
104
- ToolbarItem {
105
- Button(action: addItem) {
106
- Label("Add Item", systemImage: "plus")
107
- }
108
- }
109
- }
110
- Text("Select an item")
111
- }
112
- }
113
-
114
- private func addItem() {
115
- withAnimation {
116
- let newItem = Item(context: viewContext)
117
- newItem.timestamp = Date()
118
-
119
-
120
- do {
121
- try viewContext.save()
122
- } catch {
123
- // Replace this implementation with code to handle the error appropriately.
124
- // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
125
- let nsError = error as NSError
126
- fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
127
- }
128
- }
129
- }
130
-
131
- private func deleteItems(offsets: IndexSet) {
132
- withAnimation {
133
- offsets.map { items[$0] }.forEach(viewContext.delete)
134
-
135
- do {
136
- try viewContext.save()
137
- } catch {
138
- // Replace this implementation with code to handle the error appropriately.
139
- // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
140
- let nsError = error as NSError
141
- fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
142
- }
143
- }
144
- }
145
- }
146
-
147
- private let itemFormatter: DateFormatter = {
148
- let formatter = DateFormatter()
149
- formatter.dateStyle = .short
150
- formatter.timeStyle = .medium
151
- return formatter
152
- }()
153
-
154
- struct AnotherView: View {
155
-
156
- @Environment(\.managedObjectContext) private var viewContext
157
- @State var textEditorText = ""
158
- var item: Item
159
-
160
- init(item: Item) { _textEditorText = State(initialValue: item.text ?? "")
161
- self.item = item }
162
-
163
- var body: some View {
164
- TextEditor(text: $textEditorText).frame(width: .infinity, height: .infinity)
165
-
166
- .toolbar { ToolbarItem(placement: .navigationBarLeading){ Button(action: {additem()}) {Image(systemName: "heart")}} }
167
- }
168
-
169
- func additem () {
170
- withAnimation {
171
- item.text = textEditorText
172
-
173
- do {
174
- try viewContext.save()
175
- } catch {
176
- let nsError = error as NSError
177
- fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
178
- }
179
- }
180
- }
181
- }
182
-
183
- struct AnotherAnotherView: View {
184
- @Environment(\.managedObjectContext) private var viewContext
185
- @State var textEditorText = ""
186
-
187
- var body: some View {
188
- TextEditor(text: $textEditorText).frame(width: .infinity, height: .infinity)
189
-
190
- .toolbar { ToolbarItem(placement: .navigationBarLeading){ Button(action: {additem()}) {Image(systemName: "heart")}} }
191
- }
192
-
193
- func additem () {
194
- withAnimation {
195
- let newItem = Item(context: viewContext)
196
- newItem.timestamp = Date()
197
- newItem.text = textEditorText
198
-
199
- do {
200
- try viewContext.save()
201
- } catch {
202
- let nsError = error as NSError
203
- fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
204
- }
205
- }
206
- }
207
- }
208
-
209
- struct ContentView_Previews: PreviewProvider {
78
+
79
+ struct ContentView_Previews: PreviewProvider {
210
80
  static var previews: some View {
211
81
  ContentView()
212
82
  }
@@ -229,4 +99,4 @@
229
99
  ### 追記
230
100
 
231
101
  ![イメージ説明]
232
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-04-03/3e203577-941d-48d4-ba30-b4f52d8663a9.jpeg)
102
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-04-03/9c21ac0c-227f-43a6-b6ee-d068f60da8c2.jpeg)

10

コード修正

2023/04/03 09:31

投稿

sato31
sato31

スコア8

test CHANGED
File without changes
test CHANGED
@@ -24,16 +24,6 @@
24
24
  ### 該当のソースコード
25
25
 
26
26
  ```SwiftUI
27
-
28
- //
29
- // ContentView.swift
30
- // try15 c
31
- //
32
- // Created by Yuki Sasaki on 2023/04/02.
33
- //
34
-
35
- import SwiftUI
36
- import CoreData
37
27
 
38
28
  struct ContentView: View {
39
29
  @Environment(\.managedObjectContext) private var viewContext

9

画像追加

2023/04/03 09:30

投稿

sato31
sato31

スコア8

test CHANGED
File without changes
test CHANGED
@@ -238,4 +238,5 @@
238
238
 
239
239
  ### 追記
240
240
 
241
+ ![イメージ説明]
241
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-04-03/7074d1a5-3f03-4d5b-84d0-9627b1d54883.png)
242
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-04-03/3e203577-941d-48d4-ba30-b4f52d8663a9.jpeg)

8

追記とコード修正

2023/04/03 09:28

投稿

sato31
sato31

スコア8

test CHANGED
File without changes
test CHANGED
@@ -24,65 +24,203 @@
24
24
  ### 該当のソースコード
25
25
 
26
26
  ```SwiftUI
27
+
28
+ //
29
+ // ContentView.swift
30
+ // try15 c
31
+ //
32
+ // Created by Yuki Sasaki on 2023/04/02.
33
+ //
34
+
35
+ import SwiftUI
36
+ import CoreData
37
+
27
38
  struct ContentView: View {
28
39
  @Environment(\.managedObjectContext) private var viewContext
29
40
 
41
+ @FetchRequest(
42
+ sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],
43
+ animation: .default)
44
+ private var items: FetchedResults<Item>
45
+
46
+ private let list1 = [
47
+ SampleStruct(sectionTitle: "A", listContent: ["elementA1"],systemNameText: "pin", folderRowName: "a"),
48
+ SampleStruct(sectionTitle: "B", listContent: ["elementB1"],systemNameText: "pin", folderRowName: "a"),
49
+ SampleStruct(sectionTitle: "C", listContent: ["elementC1"],systemNameText: "pin", folderRowName: "a"),
50
+ ]
51
+
52
+ var body: some View {
53
+ NavigationView {
54
+ List {
55
+ ForEach(list1) { item in
56
+ Section(item.sectionTitle) {
57
+ ForEach(item.listContent, id: \.self) { item2 in NavigationLink{ArrayView()}label: {
58
+ SampleStruct(sectionTitle: "A", listContent: ["elementA1"],systemNameText: "pin", folderRowName: "a")}
59
+ }
60
+ .toolbar {
61
+ ToolbarItem(placement: .navigationBarTrailing) {
62
+ EditButton()
63
+ }
64
+ }
65
+ }
66
+ }
67
+ }
68
+ .navigationTitle("A")
69
+ }
70
+ }
71
+ }
72
+
73
+ struct SampleStruct : View, Identifiable {
74
+ let id = UUID()
75
+ let sectionTitle: String
76
+ let listContent: [String]
77
+ let systemNameText: String
78
+ let folderRowName: String
79
+ var body: some View {
80
+ HStack{
81
+ Image(systemName: systemNameText)
82
+ Text(folderRowName)
83
+ Text("\(systemNameText.count)")
84
+ }
85
+ }
86
+ }
87
+
88
+ struct ArrayView: View {
89
+ @Environment(\.managedObjectContext) private var viewContext
90
+
30
91
  @FetchRequest(
31
92
  sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: false)],
32
93
  animation: .default)
33
94
  private var items: FetchedResults<Item>
34
- var folderRow: FolderRow
95
+
35
-
36
96
  var body: some View {
37
97
  NavigationView {
38
- List{Section(header: Text("A")){ForEach(items) { item in NavigationLink(destination: ArrayView(arrayViewArray: folderRow.folderRowArray), label: {FolderRow(folderLowArray: folderRowArray, folderRowSystemNameText: "gear", folderLowName: "ギア")})}} }}
39
- .toolbar {
98
+ List {
99
+ ForEach(items) { item in
100
+ NavigationLink {
101
+ AnotherView(item: item)
102
+ } label: {
103
+ Text(item.text ?? "")
104
+ }
105
+ }
106
+ .onDelete(perform: deleteItems)
107
+ }
108
+ .toolbar {
40
109
  #if os(iOS)
41
- ToolbarItem(placement: .navigationBarTrailing) {
110
+ ToolbarItem(placement: .navigationBarTrailing) {
42
- EditButton()
111
+ EditButton()
43
- }
112
+ }
44
113
  #endif
45
- ToolbarItem {
46
- Button(action: print("a")) {
47
- Label("Add Item", systemImage: "plus")
48
- }
49
- }
50
- }
51
- }
52
- }
53
-
54
- struct ArrayView: View {
55
- var arrayViewArray: [String]
56
-
57
- @FetchRequest(
58
- sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: false)],
59
- animation: .default)
60
- private var items: FetchedResults<Item>
61
-
114
+ ToolbarItem {
115
+ Button(action: addItem) {
116
+ Label("Add Item", systemImage: "plus")
117
+ }
118
+ }
119
+ }
120
+ Text("Select an item")
121
+ }
122
+ }
123
+
124
+ private func addItem() {
125
+ withAnimation {
126
+ let newItem = Item(context: viewContext)
127
+ newItem.timestamp = Date()
128
+
129
+
130
+ do {
131
+ try viewContext.save()
132
+ } catch {
133
+ // Replace this implementation with code to handle the error appropriately.
134
+ // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
135
+ let nsError = error as NSError
136
+ fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
137
+ }
138
+ }
139
+ }
140
+
141
+ private func deleteItems(offsets: IndexSet) {
142
+ withAnimation {
143
+ offsets.map { items[$0] }.forEach(viewContext.delete)
144
+
145
+ do {
146
+ try viewContext.save()
147
+ } catch {
148
+ // Replace this implementation with code to handle the error appropriately.
149
+ // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
150
+ let nsError = error as NSError
151
+ fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
152
+ }
153
+ }
154
+ }
155
+ }
156
+
157
+ private let itemFormatter: DateFormatter = {
158
+ let formatter = DateFormatter()
159
+ formatter.dateStyle = .short
160
+ formatter.timeStyle = .medium
161
+ return formatter
162
+ }()
163
+
164
+ struct AnotherView: View {
165
+
166
+ @Environment(\.managedObjectContext) private var viewContext
167
+ @State var textEditorText = ""
168
+ var item: Item
169
+
170
+ init(item: Item) { _textEditorText = State(initialValue: item.text ?? "")
171
+ self.item = item }
172
+
62
173
  var body: some View {
63
- List{Section(header: Text("テキスト"))
64
- ForEach{arrayViewArray) { index in
65
- NavigationLink(destination: Text(item.text),
174
+ TextEditor(text: $textEditorText).frame(width: .infinity, height: .infinity)
175
+
176
+ .toolbar { ToolbarItem(placement: .navigationBarLeading){ Button(action: {additem()}) {Image(systemName: "heart")}} }
177
+ }
178
+
179
+ func additem () {
180
+ withAnimation {
66
- label: {Text(item.text])})}} }}}
181
+ item.text = textEditorText
182
+
67
-
183
+ do {
184
+ try viewContext.save()
185
+ } catch {
186
+ let nsError = error as NSError
187
+ fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
188
+ }
189
+ }
190
+ }
191
+ }
192
+
68
- struct FolderRow: View {
193
+ struct AnotherAnotherView: View {
69
- var folderRowArray: [String]
194
+ @Environment(\.managedObjectContext) private var viewContext
70
- var folderRowSystemNameText: String
195
+ @State var textEditorText = ""
71
- var folderRowName: String
196
+
72
- var body: some View {
197
+ var body: some View {
73
- HStack{
74
- Image(systemName: folderRowSystemNameText)
75
- Text(folderRowName)
76
- Text("\(folderRowArray.count)")
198
+ TextEditor(text: $textEditorText).frame(width: .infinity, height: .infinity)
199
+
200
+ .toolbar { ToolbarItem(placement: .navigationBarLeading){ Button(action: {additem()}) {Image(systemName: "heart")}} }
77
- }
201
+ }
202
+
203
+ func additem () {
204
+ withAnimation {
205
+ let newItem = Item(context: viewContext)
206
+ newItem.timestamp = Date()
207
+ newItem.text = textEditorText
208
+
209
+ do {
210
+ try viewContext.save()
211
+ } catch {
212
+ let nsError = error as NSError
213
+ fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
78
- }
214
+ }
215
+ }
216
+ }
79
217
  }
80
218
 
81
219
  struct ContentView_Previews: PreviewProvider {
82
- static var previews: some View {
220
+ static var previews: some View {
83
- ContentView(folderRow: FolderRow()).environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
221
+ ContentView()
84
- }
222
+ }
85
- }
223
+ }
86
224
 
87
225
  ```
88
226
 
@@ -98,3 +236,6 @@
98
236
 
99
237
  Xcode14.1
100
238
 
239
+ ### 追記
240
+
241
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-04-03/7074d1a5-3f03-4d5b-84d0-9627b1d54883.png)

7

記述修正

2023/04/02 11:54

投稿

sato31
sato31

スコア8

test CHANGED
File without changes
test CHANGED
@@ -62,7 +62,8 @@
62
62
  var body: some View {
63
63
  List{Section(header: Text("テキスト"))
64
64
  ForEach{arrayViewArray) { index in
65
- NavigationLink(destination: Text(item.text), label: {Text(item.text])})}} }}}
65
+ NavigationLink(destination: Text(item.text),
66
+ label: {Text(item.text])})}} }}}
66
67
 
67
68
  struct FolderRow: View {
68
69
  var folderRowArray: [String]

6

記述修正

2023/04/02 11:51

投稿

sato31
sato31

スコア8

test CHANGED
File without changes
test CHANGED
@@ -60,7 +60,9 @@
60
60
  private var items: FetchedResults<Item>
61
61
 
62
62
  var body: some View {
63
+ List{Section(header: Text("テキスト"))
64
+ ForEach{arrayViewArray) { index in
63
- List{Section(header: Text("テキスト")){arrayViewArray) { index in NavigationLink(destination: Text(item.text), label: {Text(item.text])})}} }}}
65
+ NavigationLink(destination: Text(item.text), label: {Text(item.text])})}} }}}
64
66
 
65
67
  struct FolderRow: View {
66
68
  var folderRowArray: [String]

5

コード修正 ArrayView ()追加

2023/04/02 11:38

投稿

sato31
sato31

スコア8

test CHANGED
File without changes
test CHANGED
@@ -35,7 +35,7 @@
35
35
 
36
36
  var body: some View {
37
37
  NavigationView {
38
- List{Section(header: Text("A")){ForEach(items) { item in NavigationLink(destination: folderRow.folderRowArray, label: {FolderRow(folderLowArray: folderRowArray, folderRowSystemNameText: "gear", folderLowName: "ギア")})}} }}
38
+ List{Section(header: Text("A")){ForEach(items) { item in NavigationLink(destination: ArrayView(arrayViewArray: folderRow.folderRowArray), label: {FolderRow(folderLowArray: folderRowArray, folderRowSystemNameText: "gear", folderLowName: "ギア")})}} }}
39
39
  .toolbar {
40
40
  #if os(iOS)
41
41
  ToolbarItem(placement: .navigationBarTrailing) {
@@ -50,6 +50,17 @@
50
50
  }
51
51
  }
52
52
  }
53
+
54
+ struct ArrayView: View {
55
+ var arrayViewArray: [String]
56
+
57
+ @FetchRequest(
58
+ sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: false)],
59
+ animation: .default)
60
+ private var items: FetchedResults<Item>
61
+
62
+ var body: some View {
63
+ List{Section(header: Text("テキスト")){arrayViewArray) { index in NavigationLink(destination: Text(item.text), label: {Text(item.text])})}} }}}
53
64
 
54
65
  struct FolderRow: View {
55
66
  var folderRowArray: [String]

4

コード修正

2023/04/02 10:28

投稿

sato31
sato31

スコア8

test CHANGED
File without changes
test CHANGED
@@ -28,7 +28,7 @@
28
28
  @Environment(\.managedObjectContext) private var viewContext
29
29
 
30
30
  @FetchRequest(
31
- sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],
31
+ sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: false)],
32
32
  animation: .default)
33
33
  private var items: FetchedResults<Item>
34
34
  var folderRow: FolderRow

3

名前編集

2023/04/02 10:27

投稿

sato31
sato31

スコア8

test CHANGED
File without changes
test CHANGED
@@ -17,7 +17,7 @@
17
17
 
18
18
  ---
19
19
 
20
- Missing arguments for parameters 'folderRowArray', 'systemNameText', 'folderRowName' in call
20
+ Missing arguments for parameters 'folderRowArray', 'folderRowSystemNameText', 'folderRowName' in call
21
21
 
22
22
  ```
23
23
 
@@ -35,7 +35,7 @@
35
35
 
36
36
  var body: some View {
37
37
  NavigationView {
38
- List{Section(header: Text("A")){ForEach(items) { item in NavigationLink(destination: folderRow. folderRowArray, label: {FolderRow(folderLowArray: folderRowArray, folderLowPicture: "gear", folderLowName: "ギア")})}} }}
38
+ List{Section(header: Text("A")){ForEach(items) { item in NavigationLink(destination: folderRow.folderRowArray, label: {FolderRow(folderLowArray: folderRowArray, folderRowSystemNameText: "gear", folderLowName: "ギア")})}} }}
39
39
  .toolbar {
40
40
  #if os(iOS)
41
41
  ToolbarItem(placement: .navigationBarTrailing) {

2

コード中のプロパティ名改良

2023/04/02 10:24

投稿

sato31
sato31

スコア8

test CHANGED
File without changes
test CHANGED
@@ -53,11 +53,11 @@
53
53
 
54
54
  struct FolderRow: View {
55
55
  var folderRowArray: [String]
56
- var systemNameText: String
56
+ var folderRowSystemNameText: String
57
57
  var folderRowName: String
58
58
  var body: some View {
59
59
  HStack{
60
- Image(systemName: systemNameText)
60
+ Image(systemName: folderRowSystemNameText)
61
61
  Text(folderRowName)
62
62
  Text("\(folderRowArray.count)")
63
63
  }

1

訂正、

2023/04/02 10:19

投稿

sato31
sato31

スコア8

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  ### 実現したいこと
2
2
 
3
- タップしたら、その中の配列(プロパティ)が表示されるView(セル)を表示したいです
3
+ タップしたら、その中の配列(プロパティ)を表示したいです
4
4
 
5
5
  ### 前提
6
6