英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论