对 Swift 错误感到困惑 - 无法找到并重载接受提供的参数的 'init'

baffled by Swift error - Could not find and overload for 'init' that accepts the supplied arguments

为什么会产生错误:

而这不是:

如果重要,请查看下面的实时代码:

alertController.addAction(UIAlertAction(title: "Resend Verification Email",
        style: UIAlertActionStyle.Destructive,
        handler: {
            (alert: UIAlertAction!) in
            println("without this line there is a syntax error, why?")
            self._userVerificationService?.triggerEmailVerifiectionResentForUser(PFUser.currentUser())
    }))

难不成.triggerEmailVerifiectionResentForUser

Verifiection的拼写错误

编辑: 行。所以不是那样。

这个有用吗? :

alertController.addAction(UIAlertAction(title: "Resend Verification Email", style: .Destructive) { (action) in self._userVerificationService?.triggerEmailVerifiectionResentForUser(PFUser.currentUser()) })

这是我的猜测。您的函数 triggerEmailVerifiectionResentForUser 具有 return 值。 Swift 中的单行闭包隐含地为其一行取 return 值,因此您的闭包是 returning 东西。

UIAlertAction 的初始化需要一个 return 为 Void 的闭包。由于您的闭包隐式地 returns 东西,它不符合 init 的要求,它 return 无效。

当闭包有一个额外的行时,它不再隐含地获取 return 值,所以它 returns Void 就像你想要的那样,从而匹配 UIAlertAction init 的方法签名.

One-line closure without return type