Swift 4-致命错误:索引超出范围
我需要帮助完成Swift 4中的代码: 我想洗牌我的问题数组,但它总是在一次之后崩溃,并显示以下消息: “致命错误:索引超出范围” 这是我的密码:Swift 4-致命错误:索引超出范围,swift,random,indexoutofrangeexception,Swift,Random,Indexoutofrangeexception,我需要帮助完成Swift 4中的代码: 我想洗牌我的问题数组,但它总是在一次之后崩溃,并显示以下消息: “致命错误:索引超出范围” 这是我的密码: class ThirdViewController: UIViewController { var questions = ["A", "B", "C", "D", "E"] var answers = [["1","2","3"],["2","1","3"],["3","2","1"],["1","3","2"],["2","3",
class ThirdViewController: UIViewController {
var questions = ["A", "B", "C", "D", "E"]
var answers = [["1","2","3"],["2","1","3"],["3","2","1"],["1","3","2"],["2","3","1"]]
// Variables
var rightAnswerPlacement:UInt32 = 0
var shuffled = [String]();
let randomNumber = Int(arc4random() % 5)
// Label Question
@IBOutlet weak var Label: UILabel!
// Buttons
@IBAction func Button(_ sender: UIButton) {
if (sender.tag == Int(rightAnswerPlacement)) {
print ("RIGHT")
newQuestion()
}
else {
print ("WRONG")
newQuestion()
}
}
override func viewDidAppear(_ animated: Bool) {
self.navigationController?.isNavigationBarHidden = true
newQuestion()
}
// Functions
func newQuestion() {
Label.text = questions[randomNumber] // ----------> Fatal error: Index out of range !!! -------------------------------------------------------------
rightAnswerPlacement = arc4random_uniform(3)+1
for _ in 0..<questions.count {
let randomNumber = Int(arc4random_uniform(UInt32(questions.count)))
shuffled.append(questions[randomNumber])
questions.remove(at: randomNumber)
}
// Create a Button
var Button:UIButton = UIButton()
var x = 1
for i in 1...3 {
Button = view.viewWithTag(i) as! UIButton
if (i == Int(rightAnswerPlacement)) {
Button.setTitle(answers[randomNumber][0], for: .normal)
}
else {
Button.setTitle(answers[randomNumber][x], for: .normal)
x = 2
}
}
}
class ThirdViewController:UIViewController{
风险值问题=[“A”、“B”、“C”、“D”、“E”]
变量回答=[“1”、“2”、“3”]、[“2”、“1”、“3”]、[“3”、“2”、“1”]、[“1”、“3”、“2”]、[“2”、“3”、“1”]
//变数
var rightAnswerPlacement:UInt32=0
var shuffled=[String]();
设randomNumber=Int(arc4random()%5)
//标签问题
@IBVAR标签:UILabel!
//钮扣
@iAction func按钮(\发送方:UIButton){
if(sender.tag==Int(rightAnswerPlacement)){
打印(“右”)
新问题()
}
否则{
打印(“错误”)
新问题()
}
}
覆盖函数视图显示(u动画:Bool){
self.navigationController?.isNavigationBarHidden=true
新问题()
}
//功能
func newQuestion(){
Label.text=问题[randomNumber]/-->致命错误:索引超出范围-------------------------------------------------------------
rightAnswerPlacement=arc4random_均匀(3)+1
对于uu0..arc4random()返回一个介于0到4294967295之间的随机数
drand48()返回0.0到1.0范围内的随机数
arc4random_uniform(N)返回0到N-1范围内的随机数
试一试
让randomNumber=Int(arc4random_uniform(5))问题是,当您在for
循环中通过同一数组时,删除数组的一个成员时,数组.count
会减少,并且不可避免地会出现索引超出范围的错误
此问题的常见解决方案是使用CountableRange
的reversed()
函数从头到尾遍历数组:
for _ in (0..<questions.count).reversed(){
//YOUR CODE
}
for uu(0.)中的for
为什么在newQuestion
方法中的第一个for
循环会删除您的所有问题?我删除了它,并将我的var“questions”添加到我的函数“newQuestion”中,这样它就可以工作了,但我该怎么做?不要重复两次相同的“question”?您可能的副本应该在适当的位置,而不是删除。这将避免由于更改数组大小而导致的超出范围的错误,而且计算效率也更高。@Jamie很高兴听到这一点。如果它对您有效,请接受我的回答。在这种情况下,这不是问题。因为CountableRange
中的值不正确即使使用了顺序也无所谓。