Search code examples
swiftelcimagepickercontroller

Why I am getting ERROR: Type 'Any' has no subscript members when trying to use ELCimagepickercontroller


I am new to swift, I am using swift 3. I am trying to pick multiple image from photo library and I am using ELCimagepickercontroller However when I am trying to read the images from the array I got error: Type 'Any' has no subscript members My Code as below: please let me know what's wrong

func elcImagePickerController(_ picker: ELCImagePickerController!, didFinishPickingMediaWithInfo info: [Any]!) {   
            self.dismiss(animated: true, completion: nil)
            var i = 0
            for item in info as [AnyObject]
            {
                i += 1

 var imageview = UIImageView(image: (info[UIImagePickerControllerOriginalImage] as? [String]))         
                     // var name = .uiImageJPEGRepresentation()!
            }
}

Solution

  • Since the info parameter is an array of dictionaries, you need to properly cast item in your for loop.

    func elcImagePickerController(_ picker: ELCImagePickerController, didFinishPickingMediaWithInfo info: [Any]) {
        self.dismiss(animated: true, completion: nil)
    
        for item in info as [String : Any]
        {
            if let image = item[UIImagePickerControllerOriginalImage] as? UIImage {
                var imageview = UIImageView(image: image)
            }
        }
    }
    

    There were several other issues in your code. Don't needlessly add !. In fact, avoid all uses of ! until you fully comprehend their proper use. Until then, each use is a potential crash.