gpt4 book ai didi

ios - 我不断收到 : fatal error: Index out of range

转载 作者:行者123 更新时间:2023-11-28 10:06:13 25 4
gpt4 key购买 nike

我正在尝试为教程制作一个简单的彩票应用程序,有 5 个主球图像将显示 1 - 50 中的任何数字和两个星形球图像将显示 1 - 9 中的任何数字但出于某种原因我收到错误 fatal error :索引超出范围。谁能从我下面的代码中看出我为什么会收到此错误?我在 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])
}
}

最佳答案

根据您的问题,您有 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

查看更多关于 Ranges 的信息,例如 Closed Range (...) 和一个 Half-open Range (..<) .

关于ios - 我不断收到 : fatal error: Index out of range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53090825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com