Swift 3:修复问题Type" className"不符合协议' UIDocumentPickerDelegate'

时间:2016-12-12 22:12:29

标签: swift swift3 xcode8 icloud

我将我的代码从Swift 2迁移到Swift 3但是我的代码抛出了这个错误:输入" className"不符合协议' UIDocumentPickerDelegate'。我可以看到我的文档的很多部分,但是错误仍然存​​在,

extension className: UIDocumentMenuDelegate {
    //Opening the Document Menu
    func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
        documentPicker.delegate = self
        print ("documentMenu")
        self.present(documentPicker, animated: true, completion: nil)
    }

    func documentMenuWasCancelled(_ documentMenu: UIDocumentMenuViewController) {

    }
}
//Using UIDocumentPickerDelegate, error persists here.
extension className: UIDocumentPickerDelegate {

    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
        if(controller.documentPickerMode == UIDocumentPickerMode.import){
          print ("success")
        }
    }

}

class className: BaseViewController{

...Tons of code here...

    @IBAction func importKeyButtonPressed(_ sender: UIButton) {
    let importMenu = UIDocumentMenuViewController(documentTypes: ["public.data","public.text","public.content"], in: UIDocumentPickerMode.import)
    var documentPicker = UIDocumentPickerViewController(documentTypes: ["public.txt"], in: UIDocumentPickerMode.import)
    documentPicker.delegate = self
    documentPicker.modalPresentationStyle = UIModalPresentationStyle.fullScreen
    self.present(documentPicker, animated: true, completion: nil)
}

我该如何解决?,我已经使用了procotol所需的所有方法。感谢

1 个答案:

答案 0 :(得分:-1)

我修改了此链接上有类似问题的信息。

Cannot conform to STPAddCardViewControllerDelegate since Xcode 8 GM on Swift 3

问题是swift编译器比不能自动识别某些信息。所以在这种情况下:

didPickDocumentAt url:URL

然后我将问题跟到这个链接:

https://bugs.swift.org/browse/SR-2596

在这个链接中,信息是Swift 3错误的数据类型,所以我再研究一下并进入这个页面。在这种情况下,错误的类型是“URL”。

然后解决方案在同一页面中。我写下来:

weak var delegate : UIDocumentPickerDelegate?

@available(iOS 8.0, *)
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: Foundation.URL ){
    //
    print("something")
}

为此我在我的计算机中重现错误,这是因为不同的导入库对数据类型具有相同的定义,在这种情况下是URL类型,Swift 3无法自动识别,也没有正确地说明错误。所以必须直接定义。