-2

I'm new to swift app development, so I am following apple's food tracker tutorial. Im trying to let the user set an image to one they have on their device. I copied their code exactly but its giving me an error saying

Cannot subscript a value of type '[String : AnyObject]' with an index of type 'UIImagePickerController.InfoKey'.

Here's my code, if you see the problem please let me know. p.s. Sorry for the code format, I don't know how to change that on stack.

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject ]){
    // The info dictionary may contain multiple representations of the image. You want to use the original.
        guard let selectedImage = info[ UIImagePickerControllerOriginalImage ] as? UIImage else {
            fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
        }
        // Set photoImageView to display the selected image.
        photoImageView.image = selectedImage
        // Dismiss the picker.
        dismiss(animated: true, completion: nil)
 }
Bappaditya
  • 9,494
  • 3
  • 20
  • 29
  • 1
    Possible duplicate of [Cannot subscript a value of type '\[String : Any\]' with an index of type 'UIImagePickerController.InfoKey'](https://stackoverflow.com/questions/51342028/cannot-subscript-a-value-of-type-string-any-with-an-index-of-type-uiimage) – Rurouni Dec 26 '18 at 01:52
  • 1
    Possible duplicate of [How to allow user to pick the image with Swift?](https://stackoverflow.com/questions/25510081/how-to-allow-user-to-pick-the-image-with-swift) – SanSolo Dec 26 '18 at 02:20

1 Answers1

0

In Swift 4.2, the delegate method has been changed to:

func imagePickerController(_ picker: UIImagePickerController, 
    didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])

And UIImagePickerControllerOriginalImage has been renamed to UIImagePickerController.InfoKey.originalImage.

You should change your method signature and the name you use for the key. Your code should work fine.

Sweeper
  • 213,210
  • 22
  • 193
  • 313