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

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


The signature of the method has changed in Swift 4.2

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

and you have to write

guard let selectedImage = info[.originalImage] as? UIImage else {    fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")}

You can figure out such terminology changes yourself by reading the documentation or by commenting out the entire method, retype the first few characters and use code completion.


I'm following also the same tutorial, the updated code looks like this:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {    // The info dictionary may contain multiple representations of the image. You want to use the original.    guard let selectedImage = info[.originalImage] 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)}


Swift 5

In latest version of swift 4 or 5 the delegate method is given below, it should work

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {    // Code here}

And use,

guard let selectedImage = info[.editedImage] as? UIImage else {}func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {    guard let selectedImage = info[.editedImage] as? UIImage else {    }}