简体   繁体   English

Swift 无法将类型“()”的值转换为预期的参数类型“() -> Void”

[英]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.

相关问题 无法将类型'()'的值转换为预期的参数'()-> void' - cannot convert value of type '()' to expected argument '() -> void' 无法将“Void”类型的值转换为预期的参数类型“(String) -> Void” - Cannot convert value of type 'Void' to expected argument type '(String) -> Void' 无法将类型'() - > Void'的值转换为预期的参数类型'(( - - > Void)?' - Cannot convert value of type '() -> Void' to expected argument type '(() -> Void)?' 无法将类型“(Void)->()”的值转换为预期的参数类型“()-> Void” - Cannot convert value of type '(Void) -> ()' to expected argument type '() -> Void' swift 4.2 无法将类型“(_)-> Void”的值转换为预期的参数类型“(()-> Void)?” - swift 4.2 Cannot convert value of type '(_) -> Void' to expected argument type '(() -> Void)?' Spritekit | SWIFT 3 | -无法将Void(aka'()'类型的值转换为预期的参数类型'unsafeRawPointer' - Spritekit | SWIFT 3 | - Cannot convert value of type Void (aka'()' to expected argument type 'unsafeRawPointer' 无法转换类型'(_)->()的值? 到预期的参数类型'@convention(block)()-> Void' - Cannot convert value of type '(_) -> ()?' to expected argument type '@convention(block) () -> Void' 无法使用完成处理程序将类型“()”的值转换为预期的参数类型“()-> Void” - Cannot convert value of type '()' to expected argument type '() -> Void' with a completionHandler 无法将类型'(_)->()'的值转换为预期的参数类型'(()-> Void)? - Cannot convert value of type '(_) -> ()' to expected argument type '(() -> Void)?' 无法将类型'(_)->()'的值转换为预期的参数类型'((Bool,Error?)-> Void)? - Cannot convert value of type '(_) -> ()' to expected argument type '((Bool, Error?) -> Void)?'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM