Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot call value of non-function type UIAlertAction

Tags:

swift

I'm not sure what's wrong with this function. I'm trying to present an alert asking if the user would like to delete the selected photo.

If the function that deletes the photo returns an error, I would like to show that error to the user.

Xcode is failing on the errorController.addAction line with the message that "cannot call value of non-function type UIAlertAction"

I'm using Swift 2

@IBAction func deletePhoto(sender: AnyObject) {
    let alertController = UIAlertController(title: "Delete Photo", message: "Are you sure you want to delete this photo?", preferredStyle: .Alert)
    let okAction = UIAlertAction(title: "OK", style: .Default) { UIAlertAction in

        self.photoGateway!.delete(self.photo!.id!, completion: { (withError: Bool) -> Void in

            if (withError == true) {
                let errorController = UIAlertController(title: "Delete Failed", message: "blah", preferredStyle: UIAlertControllerStyle.Alert)
                //  The next line is causing the error
                errorController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
                self.presentViewController(errorController, animated: true, completion: nil)
            } else {
                self.dismissViewControllerAnimated(true, completion: nil)
            }

        })

    }

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { UIAlertAction in
        print("Cancelled")
    }

    alertController.addAction(okAction)
    alertController.addAction(cancelAction)
    self.presentViewController(alertController, animated: true, completion: nil)
}

If I take out the offending line then all works well, just the user has no way of dismissing the alert

like image 317
Evonet Avatar asked May 28 '16 11:05

Evonet


1 Answers

Fixed it. Changed:

errorController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))

to:

errorController.addAction(UIKit.UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
like image 92
Evonet Avatar answered Oct 01 '22 01:10

Evonet