SwiftUI:在“if”语句中,静态方法’buildExpression’没有完全匹配的引用。

huangapple go评论75阅读模式
英文:

SwiftUI: No exact matches in reference to static method 'buildExpression' inside "if" statement

问题

我不确定是否有人在之前发布过这个问题,我搜索了一下,没有找到类似的问题。

我已经努力解决这个错误“在LoginView()(第15行)之后没有找到静态方法buildExpression的精确匹配”,已经几天了。我是SwiftUI的初学者,所以不确定为什么突然出现这个错误,我的应用之前一直正常工作。

非常感谢!

以下是我的代码:

import SwiftUI

struct ContentView: View {

@State var search_shift = ""
@State var is_add_open = false
@State var alertMessage = false

@Environment(\.dismiss) var dismiss
@EnvironmentObject var viewModel: AuthViewModel

var body: some View {
    Group {
        if (viewModel.userSession == nil) {
            LoginView() // 这里是我遇到错误的地方
        } else {
            NavigationStack {
                VStack {
                    List {
                        Section(header: Text("On Duty")) {
                            //Retreive Data
                            if(viewModel.employees.count == 0) {
                                Text("No Shifts found")
                            } else {
                                ForEach(viewModel.employees) { employee in
                                    //Employees Data
                                }
                            }
                        }
                    } //Enf of List
                }
                .searchable(text: $search_shift)
                .navigationTitle("Shifts")
                .toolbar {
                    ToolbarItemGroup(placement: .navigationBarLeading) {
                        Button {
                            self.is_add_open.toggle()
                        } label: {
                            //Label("Add", systemImage: "plus")
                            Text("Add")
                        }
                    }
                    
                    ToolbarItemGroup(placement: .navigationBarTrailing) {
                        Button {
                            //Signout
                            alertMessage = true
                        } label: {
                            Label("Signout", systemImage: "rectangle.portrait.and.arrow.forward")
                        }
                        .alert("Signout", isPresented: $alertMessage) {
                            Button("Yes", role: .destructive) {
                                viewModel.signOut()
                            }
                            Button("No", role: .cancel) {
                                //dismiss()
                            }
                        } message: {
                            Text("Are you sure you want to signout?")
                        }
                    }
                }
                .sheet(isPresented: $is_add_open) {
                    NewEmployeeView()
                }
            }
        }
    }
}
}

非常感谢!

英文:

I'm not sure if someone posted this before, I've search about it and did not find a similar problem.

I've been struggling with this erorr "No exact matches in reference to static method 'buildExpression'" after LoginView() (Line: 15) for few days now. I'm a beginner in SwiftUI so I'm not sure why it appears suddenly, my app was working just fine.

Thank you so much!

Here's my code:

import SwiftUI

struct ContentView: View {

@State var search_shift = ""
@State var is_add_open = false
@State var alertMessage = false

@Environment(\.dismiss) var dismiss
@EnvironmentObject var viewModel: AuthViewModel

var body: some View {
    Group {
        if (viewModel.userSession == nil) {
            LoginView() // Here is where I get the error
        } else {
            NavigationStack {
                VStack {
                    List {
                        Section(header: Text("On Duty")) {
                            //Retreive Data
                            if(viewModel.employees.count == 0) {
                                Text("No Shifts found")
                            } else {
                                ForEach(viewModel.employees) { employee in
                                    //Employees Data
                                }
                            }
                        }
                    } //Enf of List
                }
                .searchable(text: $search_shift)
                .navigationTitle("Shifts")
                .toolbar {
                    ToolbarItemGroup(placement: .navigationBarLeading) {
                        Button {
                            self.is_add_open.toggle()
                        } label: {
                            //Label("Add", systemImage: "plus")
                            Text("Add")
                        }
                    }
                    
                    ToolbarItemGroup(placement: .navigationBarTrailing) {
                        Button {
                            //Signout
                            alertMessage = true
                        } label: {
                            Label("Signout", systemImage: "rectangle.portrait.and.arrow.forward")
                        }
                        .alert("Signout", isPresented: $alertMessage) {
                            Button("Yes", role: .destructive) {
                                viewModel.signOut()
                            }
                            Button("No", role: .cancel) {
                                //dismiss()
                            }
                        } message: {
                            Text("Are you sure you want to signout?")
                        }
                    }
                }
                .sheet(isPresented: $is_add_open) {
                    NewEmployeeView()
                }
            }
        }
    }
}
}

Thanks a lot!

答案1

得分: 1

viewModel.employees的类型是什么?

employees是否符合Identifiable协议?

例如:

struct Employee: Identifiable {
	let id: String
}

通过修复viewModel代码中的这个问题或其他问题,很可能可以解决你的问题。我在ContentView代码中没有看到任何问题。

英文:

What type is viewModel.employees?

does employees confirm to the Identifiable protocol?

e.g.

struct Employee: Identifiable {
	let id: String
}

Either this or some other issue with the viewModel code will most likely fix your issue, I did not see any issues in the ContentView code.

huangapple
  • 本文由 发表于 2023年7月27日 17:29:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定