Struct SwiftUI-无法在结构外部引用@Published值

Struct SwiftUI-无法在结构外部引用@Published值,struct,swiftui,Struct,Swiftui,我是新来的SwiftUI,希望能在我正在开发的项目中得到一些帮助 我无法在结构外部引用已发布的值。 所有值都在结构内正确运行,使用: @observeObject @Publihsed @ObservedObject 我在ContentView中有一个结构,它链接到另一个视图,我想在其中显示数量和成本。 我尝试了各种方法来访问可以在structNewOrders中看到的值,但是我一直得到零,没有得到更新的值。 我是否缺少一个步骤或错误地执行代码 提前谢谢 class MealOrders: Ob

我是新来的
SwiftUI
,希望能在我正在开发的项目中得到一些帮助

我无法在结构外部引用已发布的值。 所有值都在结构内正确运行,使用:

  • @observeObject

  • @Publihsed

  • @ObservedObject

  • 我在
    ContentView
    中有一个结构,它链接到另一个视图,我想在其中显示数量和成本。 我尝试了各种方法来访问可以在struct
    NewOrders
    中看到的值,但是我一直得到零,没有得到更新的值。 我是否缺少一个步骤或错误地执行代码

    提前谢谢

    class MealOrders: ObservableObject {
    
        //Jollof Rice - Published
        @Published var jollof = 0
    
        //Swallow - Published
        @Published var swallow = 0
    
        //Pepper Soup - Published
        @Published var pepperSoup = 0
    
    }
    struct ContentView: View {
        var body: some View {
    
                  VStack {
                    NavigationView {
    
                        Section {
                            VStack(alignment: .leading) {
    
                            NavigationLink(destination: MenuOption()) {
                            Text("Meal Options")
                        }
    
                            Spacer()
                            .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 
                            .infinity, alignment: .topLeading)
    
                            }.padding() //Vstack End
    
                        }
                        Section {
    
                            VStack {
    
                                Image("africanfoods")
                                    .resizable()
                                    .frame(width: 275.0, height: 250.0)
                                    .clipShape(Circle())
                                    .overlay(Circle().stroke(Color.black, lineWidth: 5))
                                    .scaledToFit()
    
                            }
    
    
                        } //Section End
    
                        Section { //Social Media
    
                            VStack {
                                Spacer()
    
                                HStack {
                                Spacer()
                                    Image("facebook")
                                        .resizable()
                                        .frame(width:40, height: 40)
                                        .scaledToFit()
                                        .padding()
    
                                    Image("instergram")
                                    .resizable()
                                    .frame(width:40, height: 40)
                                    .scaledToFit()
                                    .padding()
    
    
                                    Image("youtube")
                                            .resizable()
                                            .frame(width:40, height: 40)
                                            .scaledToFit()
                                            .padding()
    
                                    Image("twitter")
                                            .resizable()
                                            .frame(width:40, height: 40)
                                            .scaledToFit()
                                            .padding(.horizontal, 50)
    
                            }
    
                        } //Vstack End
                    } //Social Media
                        .navigationBarTitle(Text("African Foods Center"))
    
                }   //NavView End
        } //VStack End
        }
    }
    
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    
    struct MenuOption: View {
    
        //Object Observed Declarations
        @ObservedObject var meals = MealOrders()
    
        //Meal Prices
        @State private var jolloRiceUnitCost = 14.95 //Jollof Rice Unit Cost
    
        @State private var swallowUnitCost = 12.50 //Swallow Unit Cost
    
        @State private var pepperSoupUnitCost = 13.50 //Pepper Soup Unit Cost
    
        @State private var deliveryCost = 5.99 //Delivery Cost
    
    
        @State private var menuItem = 0
    
        var menuItems = ["Rice Meals", "Swallow Meals", "Pepper Soups"]
    
    
        //Meals Calculator
        var calculateMeal: Double {
    
            var orderCost = 0.0
    
            //Jollof Rice
            if menuItems[menuItem] == "Rice Meals" {
                 orderCost = self.jolloRiceUnitCost * Double(self.meals.jollof)
                    return orderCost
    
            //Swallow - Pounded Yam / Eba
            } else if menuItems[menuItem] == "Swallow Meals" {
                orderCost = self.swallowUnitCost * Double(self.meals.swallow)
    
            } else if menuItems[menuItem] == "Pepper Soups" {
                orderCost = self.pepperSoupUnitCost * Double(self.meals.pepperSoup)
    
            }
    
                return orderCost
        }
    
    
        var body: some View {
    
            VStack(alignment: .leading) {
    
                Section(header: Text("Select Meal").padding(.horizontal, 25)) {
    
                    Picker("Select Item", selection: $menuItem) {
                                 ForEach(0 ..< menuItems.count) {
                                     Text(self.menuItems[$0])
    
                                 }
                             }.pickerStyle(SegmentedPickerStyle())
                              .padding(.horizontal, 25)
    
                             if menuItems[menuItem] == "Rice Meals" {
                                 Text("Rice Meals:")
                                    .foregroundColor(.black)
                                    .padding(.horizontal, 25)
    
                                Spacer().frame(height:20)
                                    `Text("Rice meals consist of Jollof Rice, Rice and Stew, Fried 
                                Rice. You can option in meat, fish and or plantain with any of the meals")`
                                    .foregroundColor(.blue)
                                    .lineLimit(nil)
                                    .padding(.horizontal, 25)
    
                                Spacer().frame(height:60)
    
                                    //Insert image - Jollof Rice
                                    Image("jollofRice")
                                        .resizable()
                                        .frame(width:250, height: 250)
                                        .scaledToFit()
                                        .border(Color.black, width: 4)
                                        .padding(.horizontal, 100)
    
                                Spacer().frame(height:20)
    
                                HStack {
                                Stepper("Quantity (Max 5)",value: $meals.jollof, in: 0...5)
                                    .padding(.horizontal, 45)
                                    Text("\(meals.jollof): £\(calculateMeal, specifier: "%.2f")")
                                    .padding()
    
                                }.padding()
    
    
                                Section { //Button Order
    
                                    VStack {//Add Stepper for Quantity
                                        Spacer()
    
                                        HStack(alignment: .center, spacing: 140) {
                                            Spacer()
                                            Button(action: {}) {
    
                                                NavigationLink(destination: Text("Jollof::  \(meals.jollof), £\(calculateMeal, specifier:  "%.2f")")) {
                                                    Text("Create Order")
                                                        .font(.system(size: 14))
                                                        .padding()
                                                        .background(Color.blue)
                                                        .foregroundColor(.white)
                                                        .cornerRadius(6)
    
                                                }.padding()
                                                Spacer()
    
                                            }
                                        }
    
    
                                    } //Vstack End
    
    
                                } //Button Section End
    
                             } else if menuItems[menuItem] == "Swallow Meals" {
                                 Text("Swallow")
                                    .foregroundColor(.green)
                                    .padding(.horizontal, 25)
    
                                Spacer().frame(height:20)
                                    `Text("Swallow is the colloqual word for Pounded Yam, Eba, Fufu or Amala. This is complemented with soups, such as Egusi, Okra etc. You could have this meal with either meat or fish")`
    
                                        .foregroundColor(.blue)
                                        .lineLimit(nil)
                                        .padding(.horizontal, 25)
    
                                Spacer().frame(height:60)
    
                                    //Inset Image
                                    Image("poundedYamEgusi")
                                        .resizable()
                                        .frame(width:250, height:250)
                                        .scaledToFit()
                                        .border(Color.black, width: 4)
                                        .padding(.horizontal, 100)
    
                                HStack {//Add Stepper for Quantity
                                    Stepper("Quantity (Max 5)",value: $meals.swallow, in: 0...5)
                                        .padding(.horizontal, 45)
                                    Text("\(meals.swallow): £\(calculateMeal, specifier: "%.2f")")
                                    .padding()
                                }.padding()
    
    
                                //Insert Order Button
                                Section {
    
                                    VStack {
    
                                        Spacer()
    
                                        HStack(alignment: .center, spacing: 140) {
                                            Spacer()
                                                Button(action: {}) {
    
                                                    NavigationLink(destination: Text("Swallow:  \(meals.swallow), £\(calculateMeal, specifier:  "%.2f")")) {
                                                        Text("Create Order")
                                                            .font(.system(size: 14))
                                                            .padding()
                                                            .background(Color.blue)
                                                            .foregroundColor(.white)
                                                            .cornerRadius(6)
    
                                                    }.padding()
                                                    Spacer()
                                                }
                                        }
    
                                    } //VStack End
    
    
                                } //Section End
    
                             } else if menuItems[menuItem] == "Pepper Soups" {
                                 Text("Pepper Soup")
                                    .foregroundColor(.red)
                                    .padding(.horizontal, 25)
    
                                Spacer().frame(height:20)
                                    `Text("Pepper soup is normally taken after one has had either Pounded Yam or rice meals. Depending on the audeience, the soup can be lightly spicy to very hot. Pepper soup can be had with either fish or meat")`
                                        .foregroundColor(.blue)
                                        .lineLimit(nil)
                                        .padding(.horizontal, 25)
    
                                Spacer().frame(height:60)
    
                                //Insert Image
                                Image("pepperSoupYam")
                                    .resizable()
                                    .frame(width:250, height:250)
                                    .scaledToFit()
                                    .border(Color.black, width: 4)
                                    .padding(.horizontal, 100)
    
                                HStack {//Add Stepper Quantity for Pepper Soup
                                    Stepper("Quantity (5 Max)",value: $meals.pepperSoup, in: 0...5)
                                        .padding(.horizontal, 45)
                                    Text("\(meals.pepperSoup): £\(calculateMeal, specifier: "%.2f")")
                                    .padding()
    
                                }.padding()
    
                                //Inset Order Button
                                Section {
    
                                    VStack {
    
                                        Spacer()
    
                                        HStack(alignment: .center, spacing: 140) {
                                            Spacer()
    
                                            Button(action: {}) {
    
                                                NavigationLink(destination: NewOrders()) {
                                                    Text("Create Order")
                                                    .font(.system(size: 14))
                                                    .padding()
                                                    .background(Color.blue)
                                                    .foregroundColor(.white)
                                                    .cornerRadius(6)
    
                                                }.padding()
                                                Spacer()
                                            }
                                        }
    
                                    }
                                }
    
                             }
    
                             Spacer()
    
                }.font(.system(size: 14))
                 .foregroundColor(.purple)
    
                } //Vstack Ending
    
    
        } //View Ending
    }
    
    
    //This sis going to be the Orders Struct
    struct NewOrders: View {
    
        @ObservedObject var orderUpdatedV2 = MealOrders()
    
        @State private var orderUpdate = MenuOption().meals.jollof
    
        var  body: some View {
    
            //let mealsU: ContentView
    
            NavigationView{
    
                VStack {
    
                    List {
    
                        Text("")
    
                    }
    
                }
            }.navigationBarTitle("Meal Order")
        }
    
    }
    
    类测量器:可观察对象{
    //约洛夫·赖斯-出版
    @已发布的var jollof=0
    //燕子出版社
    @已发布的var=0
    //胡椒汤-已出版
    @已发布的var=0
    }
    结构ContentView:View{
    var body:一些观点{
    VStack{
    导航视图{
    部分{
    VStack(对齐:。前导){
    导航链接(目标:菜单选项()){
    文本(“膳食选择”)
    }
    垫片()
    .frame(最小宽度:0,最大宽度:。无穷大,最小高度:0,最大高度:
    .infinity,对齐:。前导)
    }.padding()//Vstack End
    }
    部分{
    VStack{
    图像(“非洲食品”)
    .可调整大小()
    .框架(宽度:275.0,高度:250.0)
    .clipShape(圆())
    .overlay(圆圈().stroke(颜色.黑色,线宽:5))
    .scaledofit()
    }
    }//节结束
    第{//节社交媒体
    VStack{
    垫片()
    HStack{
    垫片()
    图像(“facebook”)
    .可调整大小()
    .框架(宽度:40,高度:40)
    .scaledofit()
    .padding()
    图像(“仪器”)
    .可调整大小()
    .框架(宽度:40,高度:40)
    .scaledofit()
    .padding()
    图像(“youtube”)
    .可调整大小()
    .框架(宽度:40,高度:40)
    .scaledofit()
    .padding()
    图片(“推特”)
    .可调整大小()
    .框架(宽度:40,高度:40)
    .scaledofit()
    .padding(.卧式,50)
    }
    }//Vstack结束
    }//社交媒体
    .navigationBarTitle(文本(“非洲食品中心”))
    }//导航视图结束
    }//VStack结束
    }
    }
    结构内容视图\u预览:PreviewProvider{
    静态var预览:一些视图{
    ContentView()
    }
    }
    结构菜单选项:视图{
    //对象观察声明
    @ObservedObject var fines=MealOrders()
    //膳食价格
    @国有私有var Jollorice单位成本=14.95//Jollof大米单位成本
    @国有私有单位成本=12.50//单位成本
    @国有私有var pepperSoupUnitCost=13.50//胡椒汤单位成本
    @国有私有var交付成本=5.99//交付成本
    @国家私有变量menuItem=0
    var menuItems=[“米饭”、“燕子餐”、“胡椒汤”]
    //膳食计算器
    var计算公式:双精度{
    var orderCost=0.0
    //乔洛夫大米
    如果menuItems[menuItem]=“米饭”{
    orderCost=self.jolloRiceUnitCost*Double(self.founds.jollof)
    退货订单成本
    //燕子捣碎山药/Eba
    }如果menuItems[menuItem]=“吞咽食物”,则为else{
    orderCost=self.snown单位成本*双倍(self.finds.snown)
    }否则如果menuItems[menuItem]=“胡椒汤”{
    orderCost=self.pepperSoupUnitCost*Double(self.Founds.pepperSoup)
    }
    退货订单成本
    }
    var body:一些观点{
    VStack(对齐:。前导){
    节(标题:文本(“选择膳食”)。填充(.horizontal,25)){
    选择器(“选择项”,选择:$menuItem){
    ForEach(0..@State private var orderUpdate = MenuOption().meals.jollof
    
    @Binding var orderUpdate: Int
    
    NavigationLink(destination: NewOrders(orderUpdate: $meals.jollof)) {