Type 'MenuItem' has no member 'example'

Hello. When I run my app, there is no error message. But when I try to upload to App Store (Build > Archive) it give me an error message:

Here is the code:

//

//  ItemDetail.swift

//  a365Planner_2020_07_20

//

//  Created by Mahmoud Rajabzadeh Mashhadi on 7/20/20.

//  Copyright © 2020 Mahmoud Rajabzadeh Mashhadi. All rights reserved.

//

import SwiftUI

struct ItemDetail: View {

    var item: MenuItem

    

    var body: some View {

        VStack {

            ZStack(alignment: .bottomTrailing) {

                Image(item.mainImage)

            }

            

            ScrollView {

            

                Text(item.recipe)

                    .padding()

                    .font(.headline)

                Text(item.ingredient)

                   .padding()

                Text(item.instruction)

                    .padding()

                Text(item.emTipTitle)

                    .padding()

                    .font(.headline)

                Text(item.emTip)

                    .padding()

                Spacer()

            }

        }.navigationBarTitle(Text(item.name), displayMode: .inline)

    }

}

struct ItemDetail_Previews: PreviewProvider {

    static var previews: some View {

        NavigationView {

            ItemDetail(item: MenuItem.example)

        }

    }

}

thanks so much.

Answered by robnotyou in 726081022

You will need to share your definition of MenuItem

Accepted Answer

You will need to share your definition of MenuItem

Hello. This code runs well but when I try to upload to App Store, it gives me an error: Type 'MenuItem' has no member 'example'

Please see the code below.

Thanks for your time in advance.

Thanks.

...............

// // ItemRow.swift // a365Planner_2020_07_20 // // Created by Mahmoud Rajabzadeh Mashhadi on 7/20/20. // Copyright © 2020 Mahmoud Rajabzadeh Mashhadi. All rights reserved. //

import SwiftUI

struct ItemRow: View { var item: MenuItem

var body: some View {
    
    NavigationLink(destination: ItemDetail(item: item)) {
        HStack {
            Image(item.thumbnailImage)
                .clipShape(Circle())
                .overlay(Circle().stroke(Color.blue, lineWidth: 2))
            Text(item.name)
        }
    }
}

}

struct ItemRow_Previews: PreviewProvider { static var previews: some View { ItemRow(item: MenuItem.example) } }

................................................

my content view is below

// // ContentView.swift // a365Planner_2020_07_20 // // Created by Mahmoud Rajabzadeh Mashhadi on 7/20/20. // Copyright © 2020 Mahmoud Rajabzadeh Mashhadi. All rights reserved. //

import SwiftUI

struct ContentView: View {

let menu = Bundle.main.decode([MenuSection].self, from: "menu.json")

var body: some View {
    NavigationView {
        List {
            ForEach(menu) { section in 
                Section(header: Text(section.name)) {
                    ForEach(section.items) { item in ItemRow(item: item)
                    }
                }
            }
        }
        .navigationBarTitle("Full Self Drive 365")
        .listStyle(GroupedListStyle())
    }
}

}

struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }

................

my ItemRow code is below:

// // ItemRow.swift // a365Planner_2020_07_20 // // Created by Mahmoud Rajabzadeh Mashhadi on 7/20/20. // Copyright © 2020 Mahmoud Rajabzadeh Mashhadi. All rights reserved. //

import SwiftUI

struct ItemRow: View { var item: MenuItem

var body: some View {
    
    NavigationLink(destination: ItemDetail(item: item)) {
        HStack {
            Image(item.thumbnailImage)
                .clipShape(Circle())
                .overlay(Circle().stroke(Color.blue, lineWidth: 2))
            Text(item.name)
        }
    }
}

}

struct ItemRow_Previews: PreviewProvider { static var previews: some View { ItemRow(item: MenuItem.example)

}

}

You still have not posted your code for MenuItem, so it's hard to say anything more.

Somewhere, you have a "struct MenuItem", or "class MenuItem"...
...that's what we need to see.

Sorry

Here you go.

import SwiftUI

struct MenuSection: Codable, Identifiable {

var id: UUID
var name: String
var items: [MenuItem]

}

struct MenuItem: Codable, Equatable, Identifiable {

var id: UUID
var name: String
var recipe: String 
var ingredient: String
var instruction: String
var emTipTitle: String
var emTip: String

var mainImage: String {
    name.replacingOccurrences(of: " ", with: "-").lowercased()
}

#if DEBUG

static let example = MenuItem(id: UUID(), name: "Maple French Toast", recipe: "Breakfast Icecream", ingredient: "Sweet, fluffy, and served piping hot, our French toast is flown in fresh every day from Maple City, Canada, which is where all maple syrup in the world comes from. And if you believe that, we have some land to sell you…", instruction: "Cook it", emTipTitle: "Energy management", emTip: "Practice 5 minute rule")
#endif

}

struct Menu_Previews: PreviewProvider {

static var previews: some View {
   Text("Full Self Drive 365!")
}

}

Type 'MenuItem' has no member 'example'
 
 
Q