BigBoy1337 BigBoy1337 - 3 months ago 7
Swift Question

imagePickerController:didFinishPickingMediaWithInfo conflicts with optional requirement method in protocol 'UIImagePickerControllerDelegate'

Here is the full error:


Objective-C method 'imagePickerController:didFinishPickingMediaWithInfo:' provided by method
'imagePickerController(_ :didFinishPickingMediaWithInfo:)'
conflicts with optional requirement method 'imagePickerController(_:didFinishPickingMediaWithInfo:)'
in protocol 'UIImagePickerControllerDelegate'


It occurs on the first of this function in my ViewController.swift file:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]){
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
ImageView.contentMode = .ScaleAspectFit
ImageView.image = pickedImage
}

dismissViewControllerAnimated(true, completion: nil)
}


I am trying to follow this tutorial: http://www.codingexplorer.com/choosing-images-with-uiimagepickercontroller-in-swift/

from the error method, I gather that there is a method didFinishPickingMediaWithInfo that it is getting from the imagePickerController base class and it doesn't like that I'm trying to overwrite it. But thats all I know. All of the imagePickerController functions I find online look like this. What am I doing wrong?

I am using Xcode 7, if that makes a difference. ]

Screenshot 1

Answer

The correct function head is:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    <#code#>
}

Note the String instead of NSObject in the declaration of the info dictionary.

I am not sure to why the docs are saying that you have to write NSObject, but String is the correct one.

If you implement any protocol methods I would recommend using the auto completion of Xcode to make sure that you don't run into problems like this.

enter image description here

I am unsure where Xcode gets that auto completion from but it appears to always be in sync with the actual compiler which in the end is the one thing you have to worry about rather than some online apple docs! Especially in times when the frameworks are constantly changing and even the language itself is under development.