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

橙三吉。 提交于 2019-12-31 01:48:29

问题


I am trying to rebuild the Apple test App for image detection via CoreML, but I have the error:

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

extension ImageClassificationViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
        picker.dismiss(animated: true)

        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        imageView.image = image
        updateClassifications(for: image)
    }

The error comes in the line:

let image = info[UIImagePickerControllerOriginalImage] as! UIImage

回答1:


It appears you are using Xcode 10/iOS 12. The signature of the delegate method changed.

Use UIImagePickerController.InfoKey instead of String for the info keys.

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

And then use .originalImage instead of UIImagePickerControllerOriginalImage.

extension ImageClassificationViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
        picker.dismiss(animated: true)

        let image = info[.originalImage] as! UIImage
        imageView.image = image
        updateClassifications(for: image)
    }


来源:https://stackoverflow.com/questions/51460295/error-cannot-subscript-a-value-of-type-string-any-with-an-index-of-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!