Reputation: 33
I am trying to return an alert controller using a computed property, however i am receiving the error "Cannot convert value of type '() -> _' to specified type 'UIAlertController'" I am pretty new to iOS development after coming from C development and I hope someone could explain weere I'm going wrong. Code sample below :
@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
}
})
}
Upvotes: 0
Views: 125
Reputation: 11539
You are assigning the variable a block that returns a UIAlertController
but you're not executing it.
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
}()
If you want to keep it as a computed property though, you need to change the property type to var displayErrorController: (UIAlertController) -> () = { ... }
Upvotes: 2