不能分配types的值(string!,Bool,!,NSError!) – >无效的值typesUIActivityViewControllerCompletionWithItemsHandler?

我在我的项目中有以下代码行…

@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) } } } 

我不断地收到一个编译器错误“无法指定types的值”(string!,Bool,[AnyObject]!,NSError!) – > Void'到types'UIActivityViewControllerCompletionWithItemsHandler?'的值'引用下面的代码行。 ..

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

任何build议,将不胜感激。

你的types签名不符合UIActivityViewControllerCompletionWithItemsHandler的定义,它是(String?, Bool, [AnyObject]?, NSError?) -> Void 。 replace你的!? 它应该可以正常工作。

用Swift 3.0签名应该是

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

如本文所示