模糊使用下标编译器错误

时间:2017-01-07 06:07:08

标签: arrays swift swift3

下面的函数是ELCImagePicker选择多个图像并且我添加了我的代码,用于运行的代码可能在安装Parse SDK之后突然发生错误

  

“下划线的模糊使用”

在这行代码中:

if let image = item[UIImagePickerControllerOriginalImage] as? UIImage

代码:

func elcImagePickerController(_ picker: ELCImagePickerController!, didFinishPickingMediaWithInfo info: [Any]!) {

    self.dismiss(animated: true, completion: nil)
    var i = 0
    var z = 0

    var imageViews = [Any]() // array of any object used to save the selected imagesviews to it so we can display it on the viewcontroller
    for item in info as [AnyObject]
    {
        z += 0 //we used z = +0 as the arrays always starts at index 0 not 1
        i += 1  //we used another var i += 1 as UIImagePickerControllerOriginalImage starts at 1

        if let image = item[UIImagePickerControllerOriginalImage] as? UIImage
        {
            imagearray.insert(image, at: z) // array of images to be used in "Saved" function below to save the images selected to core data

            image_view = UIImageView(image: image)

            let currentImageView = UIImageView(frame: CGRect(x: 130 + (i * 10), y: 50 + (i * 5), width: 50, height: 100))
            currentImageView.contentMode = .scaleAspectFit
            currentImageView.image = image_view.image
            imageViews.append(currentImageView)
        }

    }
    var y = 0
    var imageview: AnyObject
    for _ in imageViews as [Any] {
        imageview = imageViews[y] as AnyObject
        view.addSubview(imageview as! UIView)
        y += 1
    }
}

1 个答案:

答案 0 :(得分:0)

您需要告诉编译器item的类型,因此不要将info数组转换为[AnyObject],而是将其转换为[[String:Any]]

if let imagesArray = info as? [[String:Any]] {
    for item in imagesArray {
        if let image = item[UIImagePickerControllerOriginalImage] as? UIImage {
            //Access image instance 
        }
    }
}