索引超出范围(无法进入if条件)

时间:2016-04-10 09:08:17

标签: ios swift indexof

我正在制作一个词汇应用程序。用户将被要求选择一个日期,并且在点击其中一个日期后,prepareForSegue()将一个包含大约30个词汇的数组传递给ViewController。然后这个viewController开始显示一个词汇标签,一秒后,它显示一个图像,再过一秒后,它将显示词汇的含义。

这是我的代码。

class ViewController: UIViewController {

@IBOutlet weak var vocabImage: UIImageView!
@IBOutlet weak var meaningLabel: UILabel!
@IBOutlet weak var vocabLabel: UILabel!

var vocabData = [String]()
var meaningData = [String]()

var vocabNumber : Int = 0
var sceneNumber : Int = 0

var speed : Double = 0.01

var commentSwitch = false



var audioPlayer = AVAudioPlayer()



override func viewDidLoad() {
    super.viewDidLoad()

    controlTower()
        }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func controlTower()  {
    switch sceneNumber {
    case 0:

        if vocabNumber == 0 && commentSwitch == false{
            let myAlert = UIAlertController(title: "Lets start to memorize it,머리 속에 단어를 입력하는 단계", message: "뜻을 예측해 보자구요", preferredStyle: UIAlertControllerStyle.Alert)
            let myAlertAction = UIAlertAction(title: "Start", style: UIAlertActionStyle.Default, handler: { Void in
                self.commentSwitch = true
                self.controlTower()
            })
            myAlert.addAction(myAlertAction)
            presentViewController(myAlert, animated: true, completion:nil)

        }else{

        displayVocab()

        NSTimer.scheduledTimerWithTimeInterval(speed, target: self, selector: #selector(ViewController.controlTower), userInfo: nil, repeats: false)
        print("\(vocabNumber) - 0")
        sceneNumber = 1

        }
    case 1:
        displayImage()

        NSTimer.scheduledTimerWithTimeInterval(speed, target: self, selector: #selector(ViewController.controlTower), userInfo: nil, repeats: false)
        print("\(vocabNumber) - 1")
        sceneNumber = 2

    case 2:
        displayMeaning()

        NSTimer.scheduledTimerWithTimeInterval(speed, target: self, selector: #selector(ViewController.controlTower), userInfo: nil, repeats: false)
        print("\(vocabNumber) - 2")
        sceneNumber = 3


    case 3:
        if (vocabNumber  == vocabData.count ){

            deleteAll()
            sceneNumber = 0
            vocabNumber = 0
            print("I can't get in here")

        }else {




        deleteAll()
        sceneNumber = 0



        NSTimer.scheduledTimerWithTimeInterval(speed, target: self, selector: #selector(ViewController.controlTower), userInfo: nil, repeats: false)
            print("\(vocabNumber) - 3")

            controlTower()
            vocabNumber++


        }
    default: break

    }
}

func displayVocab() {
    //sound
    read()
    vocabLabel.text = vocabData[vocabNumber]

func displayImage() {
    vocabImage.image = UIImage(named: vocabData[vocabNumber])

func displayMeaning(){

    meaningLabel.text = meaningData[vocabNumber]
    }
}

所以每次在diplay之前,它都会访问controlTower,继续显示数据,直到达到条件(vocabNumber == vocabData.count)&& (scenenumber == 3)。

这是我的问题。当vocabNumber变为31(总data.count = 31)时,它将获得

  
    

致命错误:索引超出范围

  

在     func displayVocab(){

    vocabLabel.text = vocabData[vocabNumber]
}

这里(我认为是vocabNumber)

我无法弄清楚它为什么不起作用。

即使我把

if (vocabNumber <= vocabData.count){

在函数中,这仍然无法得到案例3(如果条件)

让我知道此错误的哪部分代码是错误的。

谢谢。

0 个答案:

没有答案