0

I am trying to get the images and videos from the device and trying to declare the UIImagePickerDelegate and UINavigationControllerDelegate but on the line guard let..., it gives me an error:

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

And on the line let url = info..., it gives me the same error.

How can I handle this?

//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 {
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 3
    Yes I am using Xcode 10 and Swift 4.2. Thanks a lot !!! – Eduard Shahnazaryan Dec 13 '18 at 23:13
  • 1
    Always search on an error before posting. This has been asked and answered several times before. – rmaddy Dec 13 '18 at 23:17
  • 1
    @rmaddy Nope I have already searched and the concrete type of my error never been answered, there were similar questions concerning to UIImagePickerControllerDelegate but now Sh_Khan answered and it worked. – Eduard Shahnazaryan Dec 13 '18 at 23:21
  • 1
    The others are duplicates. They show you the correct way to write the updated image picker delegate. Just because you happen to use a few other of the keys doesn't mean you couldn't have trivially figured out that trivial difference by looking at the obvious duplicates covering the **exact** same error message. Good luck. – rmaddy Dec 13 '18 at 23:26

1 Answers1

2

Use latest syntax

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)
    }
 }
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87