I'm using SwiftUI just started. I often confused how difference between Declarative programming and Imperative programming.
Console log show error:
ForEach<Range, Int, Text> count (2) != its initial count (1).
ForEach(_:content:)
should only be used for constant data. Instead conform data toIdentifiable
or useForEach(_:id:content:)
and provide an explicitid
!
Why?How to update list dynamically?How dose SwiftUI handle data source?
struct TestView: View {
@State var n = 1
var body: some View {
VStack {
Button.init("update") {
self.n += 1
}
AView.init(n: n)
}
}
}
struct AView: View {
let n: Int
var body: some View {
List {
ForEach(0..<n) { i in
Text.init(String(i))
}
}
}
}