【问题标题】:Swift: Getting EXC_BAD_INSTRUCTION when trying to append random array elementsSwift:尝试附加随机数组元素时获取 EXC_BAD_INSTRUCTION
【发布时间】:2016-09-23 00:15:02
【问题描述】:

我在尝试将随机数组元素附加到新数组时遇到错误,“线程 1:EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP, subcode=0x0)”

调试日志显示“致命错误:索引超出范围”

//If there are more than 6 players prioritizing the event, make a random choice. garudaLocations is an array containing the players who prioritized the event "Garuda".

    if garudaLocations.count > 6 {

        var finalGarudaPlayers : [Int] = []
        let getRandom = randomSequenceGenerator(1, max: garudaLocations.count) //Tell RNG how many numbers it has to pick from.
        var randomGarudaPrioritiesIndex = Int()
        for _ in 1...6 {
            randomGarudaPrioritiesIndex = getRandom() //Generate a random number.
            finalGarudaPlayers.append(garudaLocations[randomGarudaPrioritiesIndex]) //ERROR: Thread 1: EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP, subcode=0x0)
        }
        debugPrint(finalGarudaPlayers) //Print array with the final priority Garuda members.

randomSequenceGenerator is a function I got from here,它确实可以生成随机数。

 func randomSequenceGenerator(min: Int, max: Int) -> () -> Int {
    var numbers: [Int] = []
    return {
        if numbers.count == 0 {
            numbers = Array(min ... max)
        }

        let index = Int(arc4random_uniform(UInt32(numbers.count)))
        return numbers.removeAtIndex(index)
    }
}

为了更好地理解,我正在尝试编写一个“团队制作”程序,其中玩家被自动分类到事件中,但他们可以选择他们想要优先考虑的事件。

但是,我每个活动只能有 6 人,所以目标是获取现有的 garudaLocations 数组,随机选择 6 个索引位置,然后去掉其余的玩家。

只有在我将超过 6 名玩家提交到同一活动时,我才会收到错误消息。

非常感谢任何帮助!

【问题讨论】:

    标签: ios arrays swift random


    【解决方案1】:

    你可以永远谈论一个不存在的索引。如果你这样做了,你会像现在一样崩溃。

    所以,你是说:

    garudaLocations[randomGarudaPrioritiesIndex]
    

    现在,我不知道garudaLocations 是什么。但我可以肯定地告诉你,如果randomGarudaPrioritiesIndex 不是garudaLocations 中的现有索引,你绝对会崩溃。

    因此,您可以通过记录 (print) randomGarudaPrioritiesIndex 轻松调试。

    请记住,最大的现有索引不是garudaLocations[garudaLocations.count]。它是garudaLocations[garudaLocations.count-1]。所以比较 randomGarudaPrioritiesIndexgarudaLocations.count-1。如果它更大,当您将其用作garudaLocations 上的索引时会崩溃。

    【讨论】:

    • garudaLocations 是包含优先选择该赛事的玩家的数组。 randomGarudaPrioritiesIndex 是一个包含随机数的变量(使用 randomSequenceGenerator 函数生成)。例如,通常要列出 特定 索引元素,您可以使用 (array[X]) 列出数组中的元素 X。我认为我可以使用随机数生成器代替 0,这样我就可以找到一个 random 数组元素。不是这样吗?
    • 当我 debugPrint(randomGarudaPrioritiesIndex) 我得到 6 个随机整数,如预期的那样(不在数组中)。
    • 这一切都很棒。但这似乎无关紧要;问题是在表达式garudaLocations[randomGarudaPrioritiesIndex] 中使用这些随机整数是否合法。显然,有时并非如此。
    • 我完全明白你在说什么。,现在。我将 let getRandom = randomSequenceGenerator(1, max: garudaLocations.count) 更改为 let getRandom = randomSequenceGenerator(0, max: garudaLocations.count-1) 现在一切正常.我现在明白你在说什么......因为随机数从 1 开始并一直计数到 array.count,如果它生成了顶部的数组编号,它在数组中不存在(因为数组从 0 开始)。感谢您的耐心等待。
    • 没问题。这就是为什么我在回答中强调count-1 的概念!
    猜你喜欢
    • 2015-01-31
    • 1970-01-01
    • 2018-04-28
    • 2014-07-30
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多