3

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 ?

2
  • Did you try anything ? Commented Nov 7, 2019 at 19:25
  • Ya i tried a for each but didn't add the state I resolved my issue thanks.... Commented Nov 7, 2019 at 20:35

1 Answer 1

6

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) }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

The @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.
Thanks John M I didn't add @State property

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.