简体   繁体   中英

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

I have added the library MBDocCapture to my project using CocoaPods. Now, as its readme suggests, I made my view controller conform to ImageScannerControllerDelegate and added all 4 protocol methods to my code:

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:

Type 'DocumentUploaderViewController' does not conform to protocol 'ImageScannerControllerDelegate'
Do you want to add protocol stubs?

When I press Fix , Xcode adds the didFailWithError method:

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:)'

I have already tried:

  • build
  • clean derived data
  • clean & build
  • quit Xcode, clean, build
  • reboot my Mac (10.14.3), open Xcode, clean, build

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.

To fix this, you have two options:

  1. rename your model, like MyError
  2. or change the method's declaration to 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.

  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?"

Simply click fix. And you are done .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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