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

标签 ios swift

我正在尝试为教程制作一个简单的彩票应用程序,有 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/

相关文章:

ios - Firebase 错误,即使我不使用它

ios - 将带有可见导航栏的 View Controller 弹出到带有隐藏导航栏的 View Controller 时,带有 interactivePopGestureRecognizer 的黑色区域

ios - IBDesignable 无法工作 Xcode 7 Swift 2(核心显卡)

swift - 调整 iPad2 图标图像大小 - swift

ios - 为什么每次我完全关闭应用程序时我的本地存储 (Userdefaults) 都是空的 (Swift)

ios - 使用Swift将数据写入Firebase

ios - 为登录和注销用户显示不同的标签栏项目

ios - 基于 UIScrollView 内容 View 高度不增加

ios - 如何循环结构中的项目?

iphone - 使用randNum无法访问NSArray中的对象