Thread 1 Fatal error: Index out of range ImageView

Thread 1 Fatal error: Index out of range ImageView

我在尝试弄清楚为什么我的应用程序会出现线程 1 致命错误:索引超出范围时遇到了问题。我的应用程序显示 8 张图像以开始图像幻灯片放映。出于某种原因,我收到此错误,我无法弄清楚。有没有办法消除这个错误?谁能帮帮我?

这是我的代码 balow 和屏幕截图 link 这里 :

var images = [UIImage]()

var counter = 2

var time = Timer()


@IBOutlet weak var menuButton: UIBarButtonItem!
@IBOutlet weak var ImageView: UIImageView!
@IBOutlet weak var Slider1: UISlider!
@IBAction func Slider(_ sender: UISlider) {
    _ = 0
    let value = Int(sender.value)
    ImageView.image = images[value]
}

@IBAction func NextButton(_ sender: Any) {
    Slider1.value += 1
    ImageView.image = images[Int(Slider1.value)]
    self.ImageView.animationImages = self.images
    self.ImageView.animationDuration = 15.0
    self.ImageView.animationRepeatCount = 0
    self.ImageView.startAnimating()
    UIView.transition(with: self.ImageView, duration: 5.0, options: .transitionCrossDissolve, animations: {self.ImageView.image = self.ImageView.image}, completion: nil)

}


@IBAction func PrevButton(_ sender: Any) {
    Slider1.value -= 1
    ImageView.image = images[Int(Slider1.value)]
    self.ImageView.animationImages = self.images
    self.ImageView.animationDuration = 15.0
    self.ImageView.animationRepeatCount = 0
    self.ImageView.startAnimating()

    UIView.transition(with: self.ImageView, duration: 5.0, options: .transitionCrossDissolve, animations: {self.ImageView.image = self.ImageView.image}, completion: nil)


}

 //Set Status Bar to light content (white)
override var preferredStatusBarStyle : UIStatusBarStyle {
    return .lightContent
}

override func viewDidLoad() {
    //Set Navigation Bar color Example Home, Back button
    self.navigationItem.backBarButtonItem?.tintColor = UIColor.white;

    time = Timer.scheduledTimer(withTimeInterval: 8, repeats: true) { _ in
        self.NextButton(self);



    }
super.viewDidLoad()

setup()

   images = [#imageLiteral(resourceName: "MainImage1.jpg"), #imageLiteral(resourceName: "MainImage2.jpg"), #imageLiteral(resourceName: "MainPage3.jpg"), #imageLiteral(resourceName: "MainImage4.jpg"), #imageLiteral(resourceName: "MainImage5.jpg"), #imageLiteral(resourceName: "MainImage6.jpg"), #imageLiteral(resourceName: "MainImage7.jpg"), #imageLiteral(resourceName: "MainImage8.jpg")]

    sideMenus()

    // Do any additional setup after loading the view.
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func setup(){
self.navigationController?.navigationBar.tintColor = UIColor.white
}

override var prefersStatusBarHidden: Bool{
    return false
}

var navigationBarAppearace = UINavigationBar.appearance()
 override func viewDidAppear(_ animated: Bool){
}



func sideMenus() {

    if revealViewController() != nil {

        menuButton.target = revealViewController()
        menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
        revealViewController().rearViewRevealWidth = 275
        revealViewController().rightViewRevealWidth = 160

        view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    }
}
    }

错误提示您正在访问 images 数组范围之外的元素。在 viewDidLoad 中,您设置了包含 8 个元素的 images 数组。这些元素的范围为 0-7。如果您尝试访问该范围之外的元素,则会出现您所看到的超出范围错误。

查看你的代码,问题是在 NextButtonPrevButton 中,你调整了滑块值和索引,但你没有检查以确保你没有过去数组边界。如果您在第一张图片上单击 Prev 按钮,则说明您超出了范围。如果您在最后一张图片上单击“下一步”按钮,则说明您超出了范围。

您需要做的是确保数组中没有负索引或索引大于或等于数组中的项目数。从用户界面的角度来看,最好的解决方案是在第一张图片上禁用 Prev 按钮,在最后一张图片上禁用 Next 按钮。

既然你问了一个例子,这里有一些代码要在递增滑块值后放入 nextButton

if slider1.value >= images.count {
    return
}

递减滑块值后 prevButton 的代码:

if slider1.value < 0 {
    return
}

现在,如果数组索引超出范围,函数将退出。

最后一件事要检查的是 images 数组正在 viewDidLoad 中填充。在 viewDidLoad 中设置断点并单步执行代码。如果加载 MainImage.jpg 文件时出现问题,数组将为空,并且在访问数组时会出现超出范围的错误。