UIImagePickerControllerDelegate didFinishPickingMediaWithInfo 给我一个错误

UIImagePickerControllerDelegate didFinishPickingMediaWithInfo giving me an error

我正在尝试从设备获取图像和视频并尝试声明 UIImagePickerDelegateUINavigationControllerDelegate 但是在行 guard let... 上,它给了我一个错误:

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

let url = info... 行,它给了我同样的错误。

我该如何处理?

//UIImagePickerControllerDelegate
extension SecondViewController: UIImagePickerControllerDelegate {
    private func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        guard let mediaType = info[UIImagePickerControllerMediaType] as? String,
            mediaType == (kUTTypeMovie as String),
            let url = info[UIImagePickerControllerMediaURL] as? URL
            else { return }

        dismiss(animated: true) {
            let player = AVPlayer(url: url)
            let vcPlayer = AVPlayerViewController()
            vcPlayer.player = player
            self.present(vcPlayer, animated: true, completion: nil)
        }
    }
}

//UINavigationControllerDelegate
extension SecondViewController: UINavigationControllerDelegate {
}

使用最新语法

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

guard let mediaType = info[.mediaType] as? String,
    mediaType == (kUTTypeMovie as String),
    let url = info[.mediaURL] as? URL
    else { return }

    dismiss(animated: true) {
        let player = AVPlayer(url: url)
        let vcPlayer = AVPlayerViewController()
        vcPlayer.player = player
        self.present(vcPlayer, animated: true, completion: nil)
    }
 }
}