简体   繁体   English

即使已实现所有方法,View Controller也不符合协议

[英]View Controller not conforming to protocol, even though all methods are implemented

I have added the library MBDocCapture to my project using CocoaPods. 我已经使用CocoaPods将库MBDocCapture添加到我的项目中。 Now, as its readme suggests, I made my view controller conform to ImageScannerControllerDelegate and added all 4 protocol methods to my code: 现在,正如其自述文件所示,我使我的视图控制器符合ImageScannerControllerDelegate并将所有4种协议方法添加到我的代码中:

extension DocumentUploaderViewController: ImageScannerControllerDelegate {
    func imageScannerController(_ scanner: ImageScannerController, didFinishScanningWithResults results: ImageScannerResults) {
        scanner.dismiss(animated: true)
    }

    func imageScannerController(_ scanner: ImageScannerController, didFinishScanningWithPage1Results page1Results: ImageScannerResults, andPage2Results page2Results: ImageScannerResults) {
        scanner.dismiss(animated: true)
    }

    func imageScannerControllerDidCancel(_ scanner: ImageScannerController) {
        scanner.dismiss(animated: true)
    }

    func imageScannerController(_ scanner: ImageScannerController, didFailWithError error: Error) {
        scanner.dismiss(animated: true)
    }
}

Now, Xcode (10.2.1) complains that I'm still missing some protocol stubs: 现在,Xcode(10.2.1)抱怨我仍然缺少一些协议存根:

Type 'DocumentUploaderViewController' does not conform to protocol 'ImageScannerControllerDelegate' 类型'DocumentUploaderViewController'不符合协议'ImageScannerControllerDelegate'
Do you want to add protocol stubs? 你想添加协议存根吗?

When I press Fix , Xcode adds the didFailWithError method: 当我按下Fix ,Xcode会添加didFailWithError方法:

func imageScannerController(_ scanner: ImageScannerController, didFailWithError error: Error) {
}

...and then complains that I have added an invalid redeclaration of the method (because it was already there!): ...然后抱怨我添加了无效的方法重新声明(因为它已经存在!):

Invalid redeclaration of 'imageScannerController(_:didFailWithError:)' “imageScannerController(_:didFailWithError :)”的重新声明无效

I have already tried: 我已经尝试过了:

  • build 建立
  • clean derived data 清理派生数据
  • clean & build 清洁和建设
  • quit Xcode, clean, build 退出Xcode,清理,构建
  • reboot my Mac (10.14.3), open Xcode, clean, build 重新启动我的Mac(10.14.3),打开Xcode,清理,构建

None of these tries helped. 这些尝试都没有帮助。
Any ideas? 有任何想法吗?

You might have an Error model (struct or class) defined in your project explicitly which is causing this issue. 您可能会在项目中明确定义Error模型(结构或类),从而导致此问题。

To fix this, you have two options: 要解决此问题,您有两种选择:

  1. rename your model, like MyError 重命名您的模型,如MyError
  2. or change the method's declaration to didFailWithError error: Swift.Error 或者将方法的声明更改为didFailWithError error: Swift.Error

This error always appears when there is a conflict in the scope of the model. 当模型范围发生冲突时,始终会出现此错误。 Current delegate stubs written for extension DocumentUploaderViewController: ImageScannerControllerDelegate are considering the Error model of your project defined in local scope, while the delegate stub expects the Error model defined in Swift. extension DocumentUploaderViewController: ImageScannerControllerDelegate编写的当前委托存根extension DocumentUploaderViewController: ImageScannerControllerDelegate正在考虑在本地作用域中定义的项目的Error模型,而委托存根需要在Swift中定义的Error模型。

  1. Search for the method in the entire project, this kind of error usually occurs when functions have similar names or are declared twice. 在整个项目中搜索方法,当函数具有相似的名称或声明两次时,通常会发生这种错误。
  2. Check if you have set the delegate to the appropriate view controller. 检查是否已将委托设置为适当的视图控制器。 Something like: 就像是:

    let scannerViewController = ImageScannerController()

    scannerViewController.imageScannerDelegate = self

Dont add methods manually. 不要手动添加方法。 Remove all methods & when the xcode complains - "Type 'DocumentUploaderViewController' does not conform to protocol 'ImageScannerControllerDelegate' Do you want to add protocol stubs?" 删除所有方法&xcode抱怨时 - “类型'DocumentUploaderViewController'不符合协议'ImageScannerControllerDelegate'你想添加协议存根吗?”

Simply click fix. 只需点击修复。 And you are done . 你完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM