Data struct not working

Hi! I'm very new to swift and I was having an issue with using data from a data model.

struct Orchard: Identifiable {

    var id = UUID()

    var title: String

    var category: String

    var year: String

    var itemID: Int

    var imageName: String

    var bannerName: String

}
let orchardData: [Orchard] = [

    Orchard(title: "iPhone 12", category: "iPhone", year: "2020", itemID: 1001, imageName: "iphone_12", bannerName: "iphone_12_wallpaper"),

    Orchard(title: "iPhone 12 Pro", category: "iPhone", year: "2020", itemID: 1002, imageName: "iphone_12_pro", bannerName: "iphone_12_pro_wallpaper"),

    Orchard(title: "iPhone 11", category: "iPhone", year: "2019", itemID: 1003, imageName: "iphone_11", bannerName: "iphone_11_wallpaper"),

    Orchard(title: "iPhone 11 Pro", category: "iPhone", year: "2019", itemID: 1004, imageName: "iphone_11_pro", bannerName: "iphone_11_pro_wallpaper"),

    Orchard(title: "iPhone XS", category: "iPhone", year: "2018", itemID: 1005, imageName: "iphone_xs", bannerName: "iphone_xs_wallpaper"),

    Orchard(title: "iPhone SE 2nd Gen", category: "iPhone", year: "2020", itemID: 1006, imageName: "iphone_8", bannerName: "iphone_se_2_wallpaper")

]

I get the error "Global 'var' declaration requires an initializer expression or an explicitly stated getter"

Any help would be greatly appreciated, thanks!

Answered by Claude31 in 690025022

Declare as optional:

var orchard: Orchard?

to solve first error.

Note: photos are great, but code itself is easier to use.

For the second, remove the parameter. You can also declare orchard inside ContentView:

struct ContentView: View {
    @State var orchard: Orchard?

could you show how you use these, and where/what line you get the error. In other words show the surrounding context of this code. Is this in a View, in a function, etc...?

Thanks for responding! Here are some photos https://imgur.com/a/KKXbUMy

I'm trying to add it to a file so I can use the data to dynamically update the UI

Seems you have not shown all the code you get the error you described. Please do not hesitate to show the whole code, I guess it may be less than a few hundreds of lines.

Concur, you do not show enough of your code.

So I can only guess at your issues.

You use ContentView(orchard: Orchard[0]) in ContentView_Previews, note carefully the uppercase Orchard[0]. This is wrong, Orchard is a type not an instance and not an array. It should at least be lowercase, orchard[0]. The other error you get is because you have "var orchard: Orchard " outside of any function or View or class. You cannot declare a variable without initialising, if it's not part of a class/struct etc..

Here are some photos of the full thing https://imgur.com/a/Z30HsHC

I really appreciate the help!

Accepted Answer

Declare as optional:

var orchard: Orchard?

to solve first error.

Note: photos are great, but code itself is easier to use.

For the second, remove the parameter. You can also declare orchard inside ContentView:

struct ContentView: View {
    @State var orchard: Orchard?
Data struct not working
 
 
Q