Swift 错误无法将“Void”类型的值转换为所需的参数类型“()->;Void`UIAlertController
很多时候,我必须向用户显示一个警报,我发现自己一遍又一遍地编写相同的代码,所以我建立了一个方便的方法 当在Swift 错误无法将“Void”类型的值转换为所需的参数类型“()->;Void`UIAlertController,swift,uialertcontroller,Swift,Uialertcontroller,很多时候,我必须向用户显示一个警报,我发现自己一遍又一遍地编写相同的代码,所以我建立了一个方便的方法 当在viewDidLoad中调用self.concility.showAlertToUser()时,我得到参数dot的错误。此操作无法将类型为Void的值转换为预期的参数类型()->Void。我不明白为什么,因为我传入了那种类型的参数。另外,我不知道我是否正在创建一个保留周期,因此我将感谢您的帮助 class ConvenienceMethods { func showAlertToUs
viewDidLoad
中调用self.concility.showAlertToUser()
时,我得到参数dot的错误。此操作
无法将类型为Void
的值转换为预期的参数类型()->Void
。我不明白为什么,因为我传入了那种类型的参数。另外,我不知道我是否正在创建一个保留周期,因此我将感谢您的帮助
class ConvenienceMethods {
func showAlertToUser(alertMessage: String = "",actionOkTitle:String, actionCancelTitle:String, controller: UIViewController, cancelAction: Bool, doAction: @escaping (() -> Void)) {
let customAlert = UIAlertController(title: "", message: alertMessage, preferredStyle: .alert)
let actionCancel = UIAlertAction(title: actionCancelTitle, style: .cancel, handler: nil)
let actionOk = UIAlertAction(title: actionOkTitle, style: .default, handler: { (action: UIAlertAction) in
doAction()
})
if cancelAction == true {
customAlert.addAction(actionCancel)
}
customAlert.addAction(actionOk)
controller.present(customAlert, animated: true, completion: nil)
}
}
class ManageFeedbackTableViewController {
let convenience = ConvenienceMethods()
override func viewDidLoad() {
super.viewDidLoad()
let doThisAction = self.segueWith(id: "segueID")
self.convenience.showAlertToUser(alertMessage: "someMessage", actionOkTitle: "OK", actionCancelTitle: "No", controller: self, cancelAction: false, doAction: doThisAction)
}
//perform an action
func segueWith(id: String) -> Void{
self.performSegue(withIdentifier: id, sender: self)
}
}
因为您要传递的是对函数的引用,而不是结果本身 替换
let doThisAction=self.segueWith(id:“segueID”)
作者:
让doThisAction={self.segueWith(id:“segueID”)}
因为您要传递的是对函数的引用,而不是结果本身
替换
let doThisAction=self.segueWith(id:“segueID”)
作者:
让doThisAction={self.segueWith(id:“segueID”)}
@bibscy,
doThisAction是一个闭包,我们可以将“{}”中的代码块分配给它,如下所示:-
让doThisAction={self.segueWith(id:“segueID”)}这将起作用。@bibscy,
doThisAction是一个闭包,我们可以将“{}”中的代码块分配给它,如下所示:-
让doThisAction={self.segueWith(id:“segueID”)}可以工作。错误消失了。你能给我指一些我可以了解更多的资源吗?我还是不知道它到底是怎么工作的。另外,请从回答中删除
^
字符声明块需要^
。您可以查看一下,如果我使用^
它返回错误“^”不是前缀一元运算符
问题是关于Swift的。插入符号和链接文章与Objective-C相关。错误消失。你能给我指一些我可以了解更多的资源吗?我还是不知道它到底是怎么工作的。另外,请从回答中删除^
字符声明块需要^
。您可以查看一下,如果我使用^
它返回错误“^”不是前缀一元运算符
问题是关于Swift的。插入符号和链接文章与Objective-C相关。