質問編集履歴

5

コード修正

2023/04/02 07:59

投稿

sato31
sato31

スコア8

test CHANGED
File without changes
test CHANGED
@@ -48,7 +48,7 @@
48
48
  NavigationLink {
49
49
  AnotherAnotherView(item: Item)
50
50
  } label: {
51
- Image(systemName: "pin")
51
+ Image(systemName: "gear")
52
52
  }
53
53
  }
54
54
  }

4

コード修正

2023/04/02 07:58

投稿

sato31
sato31

スコア8

test CHANGED
File without changes
test CHANGED
@@ -48,7 +48,7 @@
48
48
  NavigationLink {
49
49
  AnotherAnotherView(item: Item)
50
50
  } label: {
51
- Text(item.text ?? "")
51
+ Image(systemName: "pin")
52
52
  }
53
53
  }
54
54
  }

3

訂正

2023/04/02 05:09

投稿

sato31
sato31

スコア8

test CHANGED
File without changes
test CHANGED
@@ -123,7 +123,3 @@
123
123
  ### 補足情報(FW/ツールのバージョンなど)
124
124
 
125
125
  Xcode14.1
126
-
127
- ### 追記
128
-
129
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-04-02/49032b71-53e7-4581-b0e1-e5ae7d663170.jpeg)

2

画像追加

2023/04/02 05:02

投稿

sato31
sato31

スコア8

test CHANGED
File without changes
test CHANGED
@@ -124,3 +124,6 @@
124
124
 
125
125
  Xcode14.1
126
126
 
127
+ ### 追記
128
+
129
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2023-04-02/49032b71-53e7-4581-b0e1-e5ae7d663170.jpeg)

1

コード修正

2023/04/01 10:29

投稿

sato31
sato31

スコア8

test CHANGED
File without changes
test CHANGED
@@ -18,13 +18,76 @@
18
18
 
19
19
  ```SwiftUI
20
20
 
21
+ struct ContentView: View {
22
+ @Environment(\.managedObjectContext) private var viewContext
23
+
24
+ @FetchRequest(
25
+ sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: false)],
26
+ animation: .default)
27
+ private var items: FetchedResults<Item>
28
+
29
+ var body: some View {
30
+ NavigationView {
31
+ List {
32
+ ForEach(items) { item in
33
+ NavigationLink {
34
+ AnotherView(item: item)
35
+ } label: {
36
+ Text(item.text ?? "")
37
+ }
38
+ }
39
+ .onDelete(perform: deleteItems)
40
+ }
41
+ .toolbar {
42
+ #if os(iOS)
43
+ ToolbarItem(placement: .navigationBarTrailing) {
44
+ EditButton()
45
+ }
46
+ #endif
21
- ToolbarItem {
47
+ ToolbarItem {
22
- NavigationLink {
48
+ NavigationLink {
23
49
  AnotherAnotherView(item: Item)
24
50
  } label: {
25
- Image(systemName: "heart")
51
+ Text(item.text ?? "")
26
52
  }
53
+ }
54
+ }
55
+ Text("Select an item")
56
+ }
57
+ }
58
+
59
+ private func addItem() {
60
+ withAnimation {
61
+ let newItem = Item(context: viewContext)
62
+ newItem.timestamp = Date()
63
+
64
+
65
+ do {
66
+ try viewContext.save()
67
+ } catch {
68
+ // Replace this implementation with code to handle the error appropriately.
69
+ // 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.
70
+ let nsError = error as NSError
71
+ fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
72
+ }
73
+ }
74
+ }
75
+
76
+ private func deleteItems(offsets: IndexSet) {
77
+ withAnimation {
78
+ offsets.map { items[$0] }.forEach(viewContext.delete)
79
+
80
+ do {
81
+ try viewContext.save()
82
+ } catch {
83
+ let nsError = error as NSError
84
+ fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
85
+ }
86
+ }
87
+ }
27
88
  }
89
+
90
+ ---
28
91
 
29
92
  struct AnotherAnotherView: View {
30
93