我正在錯誤,「線程1:EXC_BAD_INSTRUCTION(代碼= EXC_1386_INVOP,子碼=爲0x0)」試圖追加隨機陣列元件到一個新的數組時。夫特:獲取EXC_BAD_INSTRUCTION當試圖追加隨機數組元素
調試日誌說:「致命的錯誤:索引超出範圍」
//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名選手參加同一個比賽時,我纔會收到錯誤。
任何幫助非常感謝!
garudaLocations是包含選擇事件作爲優先級的玩家的陣列。 randomGarudaPrioritiesIndex是一個保存隨機數的變量(由randomSequenceGenerator函數生成)。例如,您通常會使用(array [X])列出數組中的元素X.我認爲我可以在0的位置使用隨機數發生器,這樣我就可以找到一個* random *數組元素。情況並非如此嗎? –
當我debugPrint(randomGarudaPrioritiesIndex)我得到6個隨機整數,如預期的(不在數組中)。 –
所有這一切都很棒。但似乎無關緊要;問題是這些隨機整數是否合法用於表達'garudaLocations [randomGarudaPrioritiesIndex]'中。顯然,有時並非如此。 – matt