我正在尝试为教程制作一个简单的彩票应用程序,有5个主球图像将显示1-50中的任何数字,还有两个星球图像将显示1-9中的任何数字,但由于某些原因,我收到错误致命错误:索引超出范围。谁能从下面的代码中看到为什么我会收到此错误?我正在XCode 10.1中进行编码,并使用swift作为语言。预先感谢
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])
}
}
答案 0 :(得分:3)
根据您的问题,您有ballArray
和1 to 50
的球。但是,您应该知道,数组从索引0
开始。这意味着,如果元素数为50,则索引范围为0 to 49
。
这是您尝试过的:
randomPoolBallIndex1 = Int.random(in: 0...50)
poolBallView1.image = UIImage(named: ballArray[randomPoolBallIndex1])
您正在尝试从索引0 to 50
中获得一个随机球。但是,您正在尝试访问0 to 49
范围之外的元素,这意味着您将当机。
您还需要修复此代码的多个副本。
您需要做的就是更改以下行:
randomPoolBallIndex1 = Int.random(in: 0...50)
更像这样:
randomPoolBallIndex1 = Int.random(in: 0...49) // Closed range
randomPoolBallIndex1 = Int.random(in: 0..<50) // Half-open range
答案 1 :(得分:1)
球阵列中只有49个元素,而幸运球阵列中只有8个元素。
因此,您需要执行0 ... 49和0 ... 8。