I have a array of string
fakeData = ["Array A",
"Array B",
"Array C",
"Array D"]
How Can I implement this array in ListView in SwiftUI ?
I have a array of string
fakeData = ["Array A",
"Array B",
"Array C",
"Array D"]
How Can I implement this array in ListView in SwiftUI ?
First you should mark the array as State if you are going to change the data inside the view, or leave it alone, but I choose State for demonstrate:
@State var fakeData = ["Array A", "Array B", "Array C", "Array D"]
Then make a list from it with just one line of code:
List(fakeData, id:\.self) { Text($0) }
Wrap it up:
struct ContentView: View {
@State var fakeData = ["Array A", "Array B", "Array C", "Array D"]
var body: some View {
List(fakeData, id:\.self) { Text($0) }
}
}
@State property wrapper does not have to do with "representing UI". It allows the property to be modified within the View body, and triggers a UI update when the property is changed. In other words, you can display static data without the @State property wrapper, and you should only include it on an array if you are dynamically adding or removing elements.