Ios 从var返回UIAlertController

Ios 从var返回UIAlertController,ios,swift,Ios,Swift,我试图使用计算属性返回警报控制器,但收到错误“无法将类型为“()->”的值转换为指定的类型“UIAlertController”。我从C开发出来后对iOS开发非常陌生,希望有人能解释我是否出错。代码示例如下: @objc func saveButtonPressed(_ sender: UIBarButtonItem){ var displayErrorController: UIAlertController = { let controller = UIAlertCo

我试图使用计算属性返回警报控制器,但收到错误“无法将类型为“()->”的值转换为指定的类型“UIAlertController”。我从C开发出来后对iOS开发非常陌生,希望有人能解释我是否出错。代码示例如下:

@objc func saveButtonPressed(_ sender: UIBarButtonItem){
    var displayErrorController: UIAlertController = {
        let controller = UIAlertController(title: "Field not valid !",
                                           message: "Please fill out form",
                                           preferredStyle: .alert)
        controller.addAction(UIAlertAction(title: "OK", style: .ok, handler: nil))
        return controller
    }
    form.rows.forEach({
        if !$0.wasChanged {
            self.present(displayErrorController, animated: true, completion: nil)
            return
        }

    })
}

您正在为变量分配一个返回
UIAlertController
的块,但您没有执行它

var displayErrorController: UIAlertController = {
    let controller = UIAlertController(title: "Field not valid !",
                                       message: "Please fill out form",
                                       preferredStyle: .alert)
    controller.addAction(UIAlertAction(title: "OK", style: .ok, handler: nil))
    return controller
}()

但是,如果要将其保留为计算属性,则需要将属性类型更改为
var displayErrorController:(UIAlertController)->()={…}
您正在为变量分配一个返回
UIAlertController
的块,但您没有执行它

var displayErrorController: UIAlertController = {
    let controller = UIAlertController(title: "Field not valid !",
                                       message: "Please fill out form",
                                       preferredStyle: .alert)
    controller.addAction(UIAlertAction(title: "OK", style: .ok, handler: nil))
    return controller
}()

但是,如果您想将其保留为计算属性,则需要将属性类型更改为
var displayErrorController:(UIAlertController)->()={…}

非常感谢!我现在明白了!非常感谢!我现在明白了!