无法使用索引类型为'UIImagePickerController.InfoKey'的下标类型'[String:Any]'的值 [英] Cannot subscript a value of type '[String : Any]' with an index of type 'UIImagePickerController.InfoKey'

查看:141
本文介绍了无法使用索引类型为'UIImagePickerController.InfoKey'的下标类型'[String:Any]'的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Apple的Swift iOS教程.哪个抛出错误,

I'm using Apple's Swift iOS Tutorial. Which is throwing an error,

不能使用索引类型为"UIImagePickerController.InfoKey"的索引对类型为[[String:Any]"的值进行下标

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

他们定义的功能如下.

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    // 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)
}

我正在使用Xcode版本10.0 beta 3,其中包括Swift 4.2.

I'm using Xcode Version 10.0 beta 3, which includes Swift 4.2.

我想了解如何遍历文档以了解可能已更改或损坏的内容.

I'd like to understand how to traverse the docs to understand what might have changed or broken.

推荐答案

该方法的签名在Swift 4.2中已更改

The signature of the method has changed in Swift 4.2

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

你必须写

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.

这篇关于无法使用索引类型为'UIImagePickerController.InfoKey'的下标类型'[String:Any]'的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆