[英]Swift Cannot convert value of type '()' to expected argument type '() -> Void'
I am pretty new to Swift and have come across a puzzling error.我是 Swift 的新手,遇到了一个令人费解的错误。
I have a Menu inside an view:我在视图中有一个菜单:
Menu("Report an issue")
{
Button("Pothole", action: passReport(rtype: "Pothole"))
Button("Street Obstruction", action: passReport(rtype: "Street Obstruction"))
Button("Weather Damage", action: passReport(rtype: "Weather Damage"))
Button("Vehicular Accident", action: passReport(rtype: "Vehicular Accident"))
Button("Other Issue", action: passReport(rtype: "Other Issue"))
}
Then, defined within this view is a function named passReport, it looks like so:然后,在这个视图中定义了一个名为 passReport 的 function,它看起来像这样:
func passReport(rtype: String)
{
let lat1:String = "\(LocationHelper.currentLocation.latitude)"
let long1:String = "\(LocationHelper.currentLocation.longitude)"
dataManager.addReport(id: "ID goes here", latitude: lat1, longitude: long1, reportType: rtype)
}
I am getting the error:我收到错误:
Cannot convert value of type '()' to expected argument type '() -> Void'
on all five lines in my Menu, up until recently, this function had no parameters and worked just fine, I recently added the reportType field to my dataManager and added a parameter to the function, and now it doesn't work.在我的菜单中的所有五行中,直到最近,这个 function 没有参数并且工作得很好,我最近将 reportType 字段添加到我的数据管理器并向 function 添加了一个参数,现在它不起作用。 I am sure this is something simple, I checked the other questions but they didn't answer my question.我确信这很简单,我检查了其他问题,但他们没有回答我的问题。 Thanks谢谢
You need to pass a closure to the button action.您需要将闭包传递给按钮操作。 And not void like you do now.而不是像你现在做的那样无效。
You can try你可以试试
Button("Street Obstruction", action: { passReport(rtype: "Street Obstruction") })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.