问题
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