How to dynamically add items to VStack from list in SwiftUI

Issue #678

Use enumerated to get index so we can assign to item in list. Here is how I show list of device tokens in my app Push Hero

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private var textViews: some View {
let withIndex = input.deviceTokens.enumerated().map({ $0 })
let binding: (Int, Input.DeviceToken) -> Binding<String> = { index, token in
Binding<String>(
get: { token.token },
set: { self.input.deviceTokens[index].token = $0 }
)
}

return VStack {
ForEach(withIndex, id: \.element.id) { index, token in
return DeviceTokenTextView(text: binding(index, token))
}
}
}

Comments