回答編集履歴
2
コード修正
answer
CHANGED
@@ -11,6 +11,6 @@
|
|
11
11
|
TextField("Text", text: $data.array[index].text)
|
12
12
|
}
|
13
13
|
}
|
14
|
-
.onDelete
|
14
|
+
.onDelete { data.array.remove(atOffsets: $0) }
|
15
15
|
}
|
16
16
|
```
|
1
誤字修正
answer
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
追加や削除を実装するには ForEach の id: には SampleModel の id を渡す必要がありますが、一方で index がないとデータバインディングに困りますよね。
|
2
2
|
|
3
|
-
というわけで、[enumerated](https://developer.apple.com/documentation/swift/array/1687832-enumerated) を使うと良いでしょう。ただし、id: には .offset ではなく .element.id を渡す必要があります。
|
3
|
+
というわけで、両方使える [enumerated](https://developer.apple.com/documentation/swift/array/1687832-enumerated) を使うと良いでしょう。ただし、id: には .offset ではなく .element.id を渡す必要があります。
|
4
4
|
参考: [SwiftUIのForEachでViewを繰り返し表示|TAAT|note](https://note.com/taatn0te/n/n01b6894c67a3)
|
5
5
|
|
6
6
|
```swift
|