【问题标题】:Cannot subscript a value of type '[String : Any]' with an index of type 'UIImagePickerController.InfoKey'无法使用“UIImagePickerController.InfoKey”类型的索引为“[String : Any]”类型的值下标
【发布时间】:2018-12-22 19:59:11
【问题描述】:

我正在使用Apple's Swift iOS Tutorial。这是抛出一个错误,

不能使用“UIImagePickerController.InfoKey”类型的索引为“[String : Any]”类型的值下标

他们定义的函数如下。

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。

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

【问题讨论】:

  • 我注意到本教程后面的另一行也抛出了一个错误 - button.addTarget(self, action: #selector(RatingControl.ratingButtonTapped(button:)), for: .touchUpInside)。如果其他人有同样的问题——我可以通过在课程行的开头添加@objcMembers 来解决它,就像这样:@objcMembers class RatingControl: UIStackView {。见hackingwithswift.com/example-code/language/…

标签: ios swift uiimagepickercontroller


【解决方案1】:

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

您可以通过阅读documentation 或注释掉整个方法、重新输入前几个字符并使用代码完成来自己找出这些术语的变化。

【讨论】:

  • 很好,它已经清除了 IDE 中的错误。就像我刚开始做 iOS 并遵循相同教程的 OP。
  • 有人收到类似“'UIImage”的错误吗?在“as?”中不能转换为“UIImage”” ?
【解决方案2】:

我也在学习相同的教程,更新后的代码如下所示:

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

【讨论】:

    【解决方案3】:

    在 Swift 4.2 中对方法进行了一些更改。

    首先初始化这两个变量:

    var imagePicker = UIImagePickerController()
    var pickedImageProduct = UIImage()
    
    extension yourViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate{
    //create an IBAction to access Camera
        @IBAction func accessCameraBtn(_ sender: UIButton) {
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerController.SourceType.camera
            self.present(imagePicker, animated: true, completion: nil)
        }
    //create an IBAction to access Gallery
        @IBAction func accessGalleryBtn(_ sender: UIButton) {
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
            self.present(imagePicker, animated: true, completion: nil)
        }
    //Final step put this Delegate    
        func imagePickerController(_ picker: UIImagePickerController,
                                   didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    
            guard let selectedImage = info[.originalImage] as? UIImage else {
               Print("Error: \(info)")
            }
    
                pickedImageProduct = selectedImage
    
            dismiss(animated: true, completion: nil)
         }
        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    
            dismiss(animated: true, completion: nil)
        }
    
    }
    

    【讨论】:

    • 方法改得更快了,我想4.0 甚至可能更早。
    【解决方案4】:

    在 Swift 4.2 中,您可以将函数编写为:

        func photoLib()
        {
            //opens Photo Library, call the function in a @IBAction func
            let myPickerController = UIImagePickerController()
            myPickerController.delegate = self;
            myPickerController.sourceType = 
            UIImagePickerController.SourceType.photoLibrary
            myPickerController.allowsEditing = true
            present(myPickerController, animated: true)
        }
    
    
        func imagePickerController(_ picker: UIImagePickerController, 
        didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        picker.dismiss(animated: true)
    
        guard let image = info[.editedImage] as? UIImage else {
            print("No image found")
            photoLabel.text = "Photo Not Found"
            return
        }
    }
    

    【讨论】:

      【解决方案5】:

      这样使用,

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

      【讨论】:

        【解决方案6】:

        在 Swift 4 和 5 中是这样的:

        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
            guard let selectedImage = info[.editedImage] as? UIImage else {
                fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
            }
        
            self.myImageView.image = selectedImage
        
            picker.dismiss(animated: true, completion: nil)
        }
        

        【讨论】:

        • Swift 4 和 5 没有区别
        【解决方案7】:

        斯威夫特 5

        在 swift 4 或 5 的最新版本中,委托方法如下所示,它应该可以工作

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

        并使用,

        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 {
        
            }
        }
        

        【讨论】:

          【解决方案8】:

          我也在关注教程。如下更改后它正在工作。

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

          我正在使用 XCode 11 和 Swift 5。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-12-29
            • 1970-01-01
            • 2019-11-19
            相关资源
            最近更新 更多