-1

I'm having issues trying to figure out why am I getting Thread 1 Fatal error: Index out of range on my app. My app displays 8 images show to start image slideshow. For some reason I'm getting this error I can't figure it out. Is there a way to remove this error? Can anyone help me?

Here is my code balow and also a screenshot link here Thread1Error:

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())
    }
}
    }
Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66
Mr_Tech
  • 51
  • 7

1 Answers1

0

The error is telling you that you're accessing an element outside the range of the images array. In viewDidLoad you set the images array with 8 elements. These elements have the range of 0-7. If you try to access an element outside that range, you get the out of range error that you're seeing.

Looking at your code, the problem is that in NextButton and PrevButton, you adjust the slider value and index, but you don't check to make sure you haven't gone past the array bounds. If you're on the first image and click the Prev button, you're out of range. If you're on the last image and click the Next button, you're out of range.

What you have to do is make sure you don't have a negative index in the array or have an index that's greater than or equal to the number of items in the array. From a user interface standpoint, the best solution is to disable the Prev button when you're on the first image and disable the Next button when you're on the last image.

Since you asked for an example, here's some code to put in nextButton after incrementing the slider value:

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

And the code for prevButton after decrementing the slider value:

if slider1.value < 0 {
    return
}

Now the function will exit if the array index is out of range.

One last thing to check is that the images array is being filled in viewDidLoad. Set a breakpoint in viewDidLoad and step through the code. If there's a problem loading the MainImage.jpg files, the array is going to be empty, and you'll get out of range errors when you access the array.

Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66
  • Thanks for your reply can you please give me an example on how to fix this issue? – Mr_Tech Mar 01 '19 at 19:35
  • I updated the answer. – Swift Dev Journal Mar 01 '19 at 19:45
  • Thanks for the answer. Can you please show me an example where I can set the breakpoing in viewdidload as well? – Mr_Tech Mar 01 '19 at 22:01
  • To set a breakpoint in Xcode, click on the left side of the editor next to a line of code. There will be a blue arrow in the editor to indicate the breakpoint. You should set the breakpoint on the line in viewDidLoad where you set the images array. – Swift Dev Journal Mar 01 '19 at 22:09
  • Ok. Sorry if I asking for I'm new to Xcode. I have already set breakpoint next to viewDidLoad. Is there another step I need to do? – Mr_Tech Mar 01 '19 at 22:43
  • If you set a breakpoint, then you don't need to do anything else to set a breakpoint. Your question has been closed, and I already answered it. I think you could use some learning material for learning iOS development. I compiled a list in the following article: [Learning iOS Development](https://swiftdevjournal.com/learning-ios-development/) – Swift Dev Journal Mar 01 '19 at 22:48
  • Thanks, Mark I appreciate your help. I will mark my question answered. – Mr_Tech Mar 01 '19 at 22:53