Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot assign a value of type '(String!, Bool, [AnyObject]!, NSError!)->Void to a value of type UIActivityViewControllerCompletionWithItemsHandler?'

I have the following lines of code in my project...

@IBAction func shareMeme(sender: UIBarButtonItem) {

    let newMeme = save()
    let memedImage = newMeme.memedImage
    let activityViewController = UIActivityViewController(activityItems: [memedImage], applicationActivities: nil)

    presentViewController(activityViewController, animated: true, completion: nil)

    activityViewController.completionWithItemsHandler = {(type: String!, completed: Bool, returnedItems: [AnyObject]!, error: NSError!) -> Void in

        dispatch_async(dispatch_get_main_queue()){
            self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
        }
    }
}

I keep getting a compiler error "Cannot assign a value of type '(String!, Bool, [AnyObject]!, NSError!) -> Void' to a value of type 'UIActivityViewControllerCompletionWithItemsHandler?'" referring to the following line of code...

    activityViewController.completionWithItemsHandler = {(type: String!, completed: Bool, returnedItems: [AnyObject]!, error: NSError!) -> Void in

Any suggestions would be appreciated.

like image 254
ssk Avatar asked Feb 01 '26 01:02

ssk


2 Answers

Your type signature doesn't match the definition of UIActivityViewControllerCompletionWithItemsHandler, which is (String?, Bool, [AnyObject]?, NSError?) -> Void. Replace your !s with ?s and it should work fine.

like image 94
jtbandes Avatar answered Feb 02 '26 16:02

jtbandes


With Swift 3.0 the signature should be

activityViewController.completionWithItemsHandler = { (activity: UIActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) in

As shown in this post

like image 26
toddg Avatar answered Feb 02 '26 18:02

toddg