Reputation: 21
Type 'ViewController' does not conform to protocol 'UIDocumentPickerDelegate'. I can't get the UIDocumentPickerDelegate to allow it to conform. Can someone help and explain how I can fix this. Swift always does this.
import UIKit
class ViewController: UIViewController, UIDocumentPickerDelegate {
@IBOutlet var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func importFiles(sender: UIBarButtonItem) {
var documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: ["public.image"], inMode: UIDocumentPickerMode.Import)
documentPicker.delgate = self
documentPicker.modalPresentationStyle = UIModalPresentationStyle.FullScreen
self.presentedViewController(documentPicker, animated: true, completion: nil)
}
func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtUrl url: NSURL){
if (controller.documentPickerMode == UIDocumentPickerMode.Import){
self.imageView.image = UIImage(contentsOfFile: url.path!)
}
}
}
Upvotes: 1
Views: 604
Reputation: 1
I revised your information on this link that has a similar problem.
Cannot conform to STPAddCardViewControllerDelegate since Xcode 8 GM on Swift 3
The problem is the swift compiler than can´t recognize automatically some information. So in this case:
didPickDocumentAt url: URL
Then i follow the problem to this link:
https://bugs.swift.org/browse/SR-2596
In this link the information is that Swift 3 mistake data types, so i research some more and get to this page. The mistaken type in this case is "URL".
Then the solution is in the same page. I write it bellow:
weak var delegate : UIDocumentPickerDelegate?
@available(iOS 8.0, *)
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: Foundation.URL ){
//
print("algo")
}
To that matter i reproduce the error in my computer, it is caused when diferent imported libraries has the same definition for data types, in this case URL type, which Swift 3 doesnt recognize automatically neither tells correctly the error. So it has to be defined directly.
Upvotes: 0
Reputation: 1797
I was getting the same compile error while my deployment target was iOS 7.1. The reason you're probably getting this error is because UIDocumentPickerDelegate is only available for iOS 8 and higher. Also you have misspelled delegate property assigning and self.presentedViewController(documentPicker, animated: true, completion: nil)
Upvotes: 1