无法分配类型'(String!,Bool,[AnyObject]!,NSError!) - > Void的值为UIActivityViewControllerCompletionWithItemsHandler类型的值?'

时间:2015-09-16 02:27:15

标签: ios swift uiviewcontroller

我的项目中有以下几行代码......

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

我不断收到编译错误“无法分配类型的值'(String!,Bool,[AnyObject] !, NSError!) - > Void'到'UIActivityViewControllerCompletionWithItemsHandler?'”类型的值,参考以下内容代码行......

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

任何建议将不胜感激。

2 个答案:

答案 0 :(得分:4)

您的类型签名与UIActivityViewControllerCompletionWithItemsHandler的定义不匹配,即(String?, Bool, [AnyObject]?, NSError?) -> Void。将!替换为?,它应该可以正常工作。

答案 1 :(得分:3)

使用Swift 3.0,签名应为

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

this post

所示