swift2 迁移到Swift 2后,UIActivityViewController出错

x7yiwoj4  于 2022-11-06  发布在  Swift
关注(0)|答案(3)|浏览(218)

ShareButton函数在Xcode 6中工作正常,但在升级到Xcode 7后,该行中出现错误:
具有项目处理程序的平均完成时间= {
我的代码:

@IBAction func ShareButton(sender: AnyObject) {

    let things = ["Thank you"]

    let avc = UIActivityViewController(activityItems:things, applicationActivities:nil)

    avc.setValue("Salon Job HK", forKey: "subject")

    avc.completionWithItemsHandler = {

        (s: String!, ok: Bool, items: [AnyObject]!, err:NSError!) -> Void in

    }

    self.presentViewController(avc, animated:true, completion:nil)

    if let pop = avc.popoverPresentationController {

        let v = sender as! UIView // sender would be the button view tapped, but could be any view

        pop.sourceView = v

        pop.sourceRect = v.bounds

    }

}
fykwrbwg

fykwrbwg1#

只需检查Swift 2.0用户界面活动视图控制器的CompletionWithItemsHandler声明:
类型别名UIActivityViewControllerCompletionWithItemsHandler =(字符串?,布尔值,[任意对象]?,NSError?)-〉无效
请这样尝试:

avc.completionWithItemsHandler = {

    (s: String?, ok: Bool, items: [AnyObject]?, err:NSError?) -> Void in

}

或者简单地说:

avc.completionWithItemsHandler = {
    (s, ok, items, err) in

}

雨燕3

@IBAction func shareButton(sender: Any) {  
    let things = ["Thank you"]
    let avc = UIActivityViewController(activityItems: things, applicationActivities: nil)
    avc.setValue("Salon Job HK", forKey: "subject")
    avc.completionWithItemsHandler = {
        (activityType, completed, returnedItems, activityError) in
        // code
    }
    present(avc, animated: true)
}
w46czmvw

w46czmvw2#

这在Swift 3.0中再次发生了轻微变化:

UIActivityViewControllerCompletionWithItemsHandler = (UIActivityType?, Bool, [Any]?, Error?) -> Void
uurv41yg

uurv41yg3#

除了上面的答案之外,您还可以将其设置为nil,因为在调用完成时您似乎没有执行任何操作

avc.completionWithItemsHandler = nil

相关问题