本文介绍了斯威夫特按钮隐藏的致命错误:数组索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
@IBAction func endTurn(sender: UIButton) {
let index: Int = Int (arc4random_uniform(UInt32(coins.count)))
var i = Int(arc4random_uniform((3)))
for i; i < 3; i++ {
coins[i].hidden = true
coins.removeAtIndex(i)
println(i)
}
}
我有21个硬币。它的按键阵列(@IBOutlet VAR硬币:[的UIButton])。当我preSS匝,隐硬币。但是,当我有3个硬币或更少,我得到的致命错误(行:硬币[I] .hidden = TRUE)。
我需要做什么?
谢谢...
I have 21 coins. It's array of buttons (@IBOutlet var coins: [UIButton]!). When i press "endTurn", the coins hidden. But when i have 3 coins or less, i get the fatal error (the line: coins[i].hidden = true).What i need do?Thanks...
推荐答案
您试图访问超出范围的数组的索引。所以增加一个检查 I
是数组应该prevent崩溃的范围。
You are trying to access an index of the array that is out of bounds. So adding a check that the i
is in the range of the array should prevent a crash.
if(i < coins.length) {
coins[i].hidden = true
coins.removeAtIndex(i)
}
3个元素[0,1,2]指标0-2变为一个数组(数组的第一个索引为0)。
An array with 3 elements [0, 1, 2] goes from index 0-2 (the first index of an array is 0).
这篇关于斯威夫特按钮隐藏的致命错误:数组索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!