Passing data from cllosure to ForEach

I'm new to swift and kind of struggling to piece swift to swiftui. Lot of my struggles come from not being find documentation for swiftui. I'm trying to pass data received from closure to ForEach loop inside modifier and its throwing error. I know its not the correct way to do it but I don't know why. I don't know why I can't pass closure call(takeReqeust) outside on .onAppear or right inside of NavigationView braces. Please advice what should I do. And send me to the right direction to look for future reference.
Thanks!!

var body: some View {
    
        NavigationView{
            List{
                Text(data)
                    .onAppear {
                        takeReqeust { (data, error) in
                            let first = data.description
                            self.data = first
                            
                           /* ForEach(self.data, id: \.self){
                                d in
                                Text(d.description.capitalized)
                            }*/
                        }
                }
                
            }.navigationBarTitle("Details")
        }

Have you looked at the Apple developer documentation? Since SwiftUI is a private Apple framework, you might want to look there.

That's a completely wrong approach, because SwiftUI is declaritive. You don't mix data fetching with the UI. I would suggest you to start watching the WWDC videos or buy a book (I can recommend you 'SwiftUI for Masterminds' as it also can tell you a lot about Swift itself: http://www.formasterminds.com/swiftui_for_masterminds/).

Back to your question: You have to fetch the data for example in your AppDelegate and then you pass the data to your view like this:

struct ContentView: View {

    @State var data: YourDataModel    // you pass the data through the init

    var body: some View {
    
        NavigationView{
            List{
                ForEach(self.data, id: \.self) { d in
                    Text(d.description.capitalized)
                }
             }   
        }.navigationBarTitle("Details")
   }
Terms of Service

Privacy Policy

Cookie Policy