为什么我会收到此错误:致命错误 - 数组索引超出范围?

时间:2021-08-05 16:23:13

I have 2 view controllers: a cameraVC and a detailVC. The camera is an AVFoundation camera that can capture and then select pictures, which then populates 4 small imageViews on my detailVC. I am trying to swipe between the two VCs so that when you hit 'yes' on the cameraVC, that then adds that to one of the 4 imageViews. I am currently using for-loops to achieve this, and below is the code I have in my detail VCs.

我有2个视图控制器:cameraVC和detailVC。相机是AVFoundation相机,可以捕捉然后选择图片,然后在我的detailVC上填充4个小图像视图。我试图在两个VC之间滑动,这样当你在cameraVC上点击“是”时,它会将其添加到4个imageView中的一个。我目前正在使用for循环来实现这一点,下面是我在详细VC中的代码。

It works when I first load the app and take a few pics, but when I swipe back to my cameraVC and then back to my detailVC, the app crashes and logs error above.

当我第一次加载应用程序并拍摄一些照片时,它可以工作,但当我刷回我的cameraVC然后回到我的detailVC时,应用程序崩溃并记录上面的错误。

In my code, I am attempting to set the imageViewArray to however many images there are in my imageArray. Then each time I go back to the detailVC, I am to clear my imageView.images and re-assign them. For some reason, it's not working. Any ideas why?

在我的代码中,我试图将imageViewArray设置为我的imageArray中的许多图像。然后每次我回到detailVC,我要清除我的imageView.images并重新分配它们。由于某种原因,它不起作用。有什么想法吗?

override func viewWillAppear(animated: Bool) {
    if imageArray.count == 1 {
        imageViewArray += [imageView1]
    } else if imageArray.count == 2 {
        imageViewArray += [imageView1,imageView2]
    } else if imageArray.count == 3 {
        imageViewArray += [imageView1,imageView2,imageView3]
    } else if imageArray.count == 4 {
        imageViewArray += [imageView1,imageView2,imageView3,imageView4]
    }

    for ima in imageViewArray {
        if ima.image != nil {
        ima.image = nil
        }
    }

    for (index, imageView) in imageViewArray.enumerate() {
        imageView.image = imageArray[index]
    }   
}

1 个解决方案

#1


1  

Add the line

添加行

imageViewArray = []

in the beginning of your viewWillAppear.

在viewWillAppear的开头。

Or change your assignment to the imageViewArray in all 4 branches to something like:

或者将所有4个分支中的imageViewArray的赋值更改为:

imageViewArray = [imageView1,imageView2,imageView3,imageView4] // removed the "+"

The problem you are encountering here is that the method gets called when the view gets presented the first time AND when some presented view gets dismissed / popped again causing your view to appear again.

您在这里遇到的问题是,当第一次显示视图时,以及当某些呈现的视图再次被解除/弹出导致您的视图再次出现时,将调用该方法。

The result of that is that the imageViewArray still contains the imageviews from the last iteration. Adding the images again causes the array to get larger.

结果是imageViewArray仍包含上次迭代的图像视图。再次添加图像会导致阵列变大。

Assume that you had 3 images the first time, now the second time you have 4 images. Therefore you enter the last else section and add 4 new elements to the array, causing it to have 7 elements while the imageArray still only has 4. Now you iterate over all imageViewArray entries and try to access the imageArray at up to index 6 while the array only contains value until index 3 -> crash.

假设您第一次有3张图像,现在第二次有4张图像。因此,您输入最后一个else部分并向数组添加4个新元素,使其具有7个元素,而imageArray仍然只有4个。现在,您遍历所有imageViewArray条目并尝试访问imageArray,最多为索引6,而数组只包含值,直到索引3 - >崩溃。

Note that it was annoying writing this answer because you have too many variables that all sound and look the same imageView, image, imageArray, imageViewArray, ima, imageViewX. Please try to find better names.

请注意,写这个答案很烦人,因为你有太多的变量,所有声音和看起来相同的imageView,image,imageArray,imageViewArray,ima,imageViewX。请尝试找到更好的名字。

#1


1  

Add the line

添加行

imageViewArray = []

in the beginning of your viewWillAppear.

在viewWillAppear的开头。

Or change your assignment to the imageViewArray in all 4 branches to something like:

或者将所有4个分支中的imageViewArray的赋值更改为:

imageViewArray = [imageView1,imageView2,imageView3,imageView4] // removed the "+"

The problem you are encountering here is that the method gets called when the view gets presented the first time AND when some presented view gets dismissed / popped again causing your view to appear again.

您在这里遇到的问题是,当第一次显示视图时,以及当某些呈现的视图再次被解除/弹出导致您的视图再次出现时,将调用该方法。

The result of that is that the imageViewArray still contains the imageviews from the last iteration. Adding the images again causes the array to get larger.

结果是imageViewArray仍包含上次迭代的图像视图。再次添加图像会导致阵列变大。

Assume that you had 3 images the first time, now the second time you have 4 images. Therefore you enter the last else section and add 4 new elements to the array, causing it to have 7 elements while the imageArray still only has 4. Now you iterate over all imageViewArray entries and try to access the imageArray at up to index 6 while the array only contains value until index 3 -> crash.

假设您第一次有3张图像,现在第二次有4张图像。因此,您输入最后一个else部分并向数组添加4个新元素,使其具有7个元素,而imageArray仍然只有4个。现在,您遍历所有imageViewArray条目并尝试访问imageArray,最多为索引6,而数组只包含值,直到索引3 - >崩溃。

Note that it was annoying writing this answer because you have too many variables that all sound and look the same imageView, image, imageArray, imageViewArray, ima, imageViewX. Please try to find better names.

请注意,写这个答案很烦人,因为你有太多的变量,所有声音和看起来相同的imageView,image,imageArray,imageViewArray,ima,imageViewX。请尝试找到更好的名字。