Darren Jay
Darren Jay

Reputation: 49

I keep Getting: fatal error: Index out of range

Im trying to make a simple lottery app for a tutorial, there are 5 main ball images that will show any number from 1 - 50 and two star ball images that will show any number from 1 - 9 but for some reason I am getting the error Fatal error: Index out of range. Can anyone see from my code below why I am getting this error please? I am coding in XCode 10.1 and using swift as the language. Thanks in advance

import UIKit

class ViewController: UIViewController {
    let ballArray = ["poolball1","poolball2","poolball3","poolball4","poolball5","poolball6","poolball7","poolball8","poolball9","poolball10","poolball11","poolball12","poolball13","poolball14","poolball15","poolball16","poolball17","poolball18","poolball19","poolball20","poolball21","poolball22","poolball23","poolball24","poolball25","poolball26","poolball27","poolball28","poolball29","poolball30","poolball31","poolball32","poolball33","poolball34","poolball35","poolball36","poolball37","poolball38","poolball39","poolball40","poolball41","poolball42","poolball43","poolball44","poolball45","poolball46","poolball47","poolball48","poolball49","poolball50"]
    let luckyBallArray = ["poolballLS1", "poolballLS2", "poolballLS3", "poolballLS4", "poolballLS5", "poolballLS6", "poolballLS7", "poolballLS8", "poolballLS9"]

    var randomPoolBallIndex: Int = 0
    var randomPoolBallIndex1: Int = 0
    var randomPoolBallIndex2: Int = 0
    var randomPoolBallIndex3: Int = 0
    var randomPoolBallIndex4: Int = 0
    var randomPoolBallIndex5: Int = 0

    var randomPoolBallLuckyIndex: Int = 0
    var randomPoolBallLuckyIndex1: Int = 0
    var randomPoolBallLuckyIndex2: Int = 0

    @IBOutlet weak var poolBallView1: UIImageView!
    @IBOutlet weak var poolBallView2: UIImageView!
    @IBOutlet weak var poolBallView3: UIImageView!
    @IBOutlet weak var poolBallView4: UIImageView!
    @IBOutlet weak var poolBallView5: UIImageView!

    @IBOutlet weak var poolLuckyView1: UIImageView!
    @IBOutlet weak var poolLuckyView2: UIImageView!

    @IBAction func buttonPressed(_ sender: UIButton) {
        randomPoolBallIndex1 = Int.random(in: 0 ... 50)
        randomPoolBallIndex2 = Int.random(in: 0 ... 50)
        randomPoolBallIndex3 = Int.random(in: 0 ... 50)
        randomPoolBallIndex4 = Int.random(in: 0 ... 50)
        randomPoolBallIndex5 = Int.random(in: 0 ... 50)

        randomPoolBallLuckyIndex1 = Int.random(in: 0 ... 9)
        randomPoolBallLuckyIndex2 = Int.random(in: 0 ... 9)

        poolBallView1.image = UIImage(named: ballArray[randomPoolBallIndex1])
        poolBallView2.image = UIImage(named: ballArray[randomPoolBallIndex2])
        poolBallView3.image = UIImage(named: ballArray[randomPoolBallIndex3])
        poolBallView4.image = UIImage(named: ballArray[randomPoolBallIndex4])
        poolBallView5.image = UIImage(named: ballArray[randomPoolBallIndex5])

        poolLuckyView1.image = UIImage(named: luckyBallArray[randomPoolBallLuckyIndex1])
        poolLuckyView2.image = UIImage(named: luckyBallArray[randomPoolBallLuckyIndex2])
    }
}

Upvotes: 0

Views: 87

Answers (2)

George
George

Reputation: 30511

From your question, you have ballArray with balls from 1 to 50. However, as you should know, Arrays start from an index of 0. This means with 50 elements, you have an index range of 0 to 49.

Here is what you tried:

randomPoolBallIndex1 = Int.random(in: 0...50)
poolBallView1.image = UIImage(named: ballArray[randomPoolBallIndex1])

You are trying to get a random ball from the index of 0 to 50. However, you are trying to access an element outside of the 0 to 49 range, meaning you will get a crash.

You will need to fix the multiple copies of this code as well.


All you need to do is change the lines of:

randomPoolBallIndex1 = Int.random(in: 0...50)

to something more like this:

randomPoolBallIndex1 = Int.random(in: 0...49) // Closed range
randomPoolBallIndex1 = Int.random(in: 0..<50) // Half-open range

See more about Ranges, such as a Closed Range (...) and a Half-open Range (..<).

Upvotes: 3

nkh
nkh

Reputation: 115

There are only 49 elements in your balls array, and 8 in your lucky balls array.

So you need to be doing 0 ... 49, and 0 ... 8.

Upvotes: 1

Related Questions