0

I am trying to pass static array data from my preview provider to main class file but its not being called and I get empty data every time. How to resolve this. Any help would be appreciated. Here is my below code that I have tried till far

    struct CountryDetails: Identifiable{
    internal init(id: Int, City: String, State: String, Country: String) {
        self.id = id
        self.City = City
        self.State = State
        self.Country = Country
    }
    let id: Int
    let City: String
    let State: String
    let Country: String
}
struct ContentView: View {
    
    @Binding var country: [CountryDetails]
    var body: some View {
        NavigationView{
            List{
                ForEach(country) { country in
                    Text(country.City)
                }
            }
            .navigationTitle("Country's Details")
        }
    }
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        OtherView()
    }
    
    struct OtherView : View {
        @State var arr:[CountryDetails] = [
            .init(id: 1, City: "Mumbai", State: "Maharashtra", Country: "India" ),
            .init(id: 2, City: "Chennai", State: "Tamil Nadu", Country: "India" ),
            .init(id: 3, City: "Bengaluru", State: "Karnataka", Country: "India" )
        ]
        var body: some View {
            NavigationView {
                ContentView(country: $arr)
            }
        }
    }
}
Arpit
  • 1
  • Does this answer your question? [SwiftUI @Binding Initialize](https://stackoverflow.com/questions/56685964/swiftui-binding-initialize) – Joakim Danielson Sep 20 '21 at 11:45
  • 1
    `@Binding` is by definition a two-way connection it doesn't store anything. If `ContentView` is your entry `View` you should remove the `@Binding` and change it to the`@State` you have placed in `PreviewProvider`. `PreviewProvider` is not meant to communicate with a simulator device or a real device, It should just be mock code to display in the screen when using Canvas. – lorem ipsum Sep 20 '21 at 14:41

1 Answers1

0

I have modified your code, you can run it on your Xcode:

import SwiftUI

struct CountryDetails: Identifiable{
let id: Int
let City: String
let State: String
let Country: String

internal init(id: Int, City: String, State: String, Country: String) {
self.id = id
self.City = City
self.State = State
self.Country = Country
}
}

struct ContentView: View {

@Binding var country: [CountryDetails]

var body: some View {
NavigationView{
    List{
       ForEach(country) { country in
           Text(country.City)
       }
    }
    .navigationTitle("Country's Details")
}
}
}

struct ContentView_Previews: PreviewProvider {
static let country: [CountryDetails] = [
.init(id: 1, City: "Mumbai", State: "Maharashtra", Country: "India" ),
.init(id: 2, City: "Chennai", State: "Tamil Nadu", Country: "India" ),
.init(id: 3, City: "Bengaluru", State: "Karnataka", Country: "India" )
]
static var previews: some View {
NavigationView {
    ContentView(country: .constant(country))
}
}
}

Entry point @Main

import SwiftUI

@main
struct testApp: App {
let country: [CountryDetails] = [
.init(id: 1, City: "Mumbai", State: "Maharashtra", Country: "India" ),
.init(id: 2, City: "Chennai", State: "Tamil Nadu", Country: "India" ),
.init(id: 3, City: "Bengaluru", State: "Karnataka", Country: "India" )
]
var body: some Scene {
    WindowGroup {
        ContentView(country: .constant(country))
    }
}
}

enter image description here

Umair Khan
  • 993
  • 8
  • 14