如何解决致命错误:索引超出范围?迅速

时间:2018-09-09 16:24:55

标签: indexing label fatal-error arc4random

我知道我必须在代码中返回某种值,这样它就不会因此线程错误而崩溃,但我无法弄清楚。请帮忙!这是代码-

@IBAction func decideBttn(_ sender: Any) {


     // if there is data in more than one Label randomly pick 1 out of 2

    if valueLbl1.text?.isEmpty == false && valueLbl2.text?.isEmpty == false
        {
            var topics = [valueLbl1.text!, valueLbl2.text!]

            pickTopic = Int(arc4random_uniform(UInt32(topics.count)))
            topics.remove(at: pickTopic)
            resultLbl.text = "\(topics[pickTopic])"  //Where fatal error occurs
            valueLbl1.text = ""
            valueLbl2.text = ""
            valueLbl3.text = ""

            return

        }


           // if all 3 Labels are used button will randomly pick 1 out of 3


      else if valueLbl1.text?.isEmpty == false && valueLbl2.text?.isEmpty == false && valueLbl3.text?.isEmpty == false

      {
       var topics = [ valueLbl1.text!, valueLbl2.text!, valueLbl3.text!]


     pickTopic = Int(arc4random_uniform(UInt32(topics.count)))

     topics.remove(at: pickTopic)
    resultLbl.text = "\(topics[pickTopic])"   
    // resetting variable value
    valueLbl1.text = ""
    valueLbl2.text = ""
    valueLbl3.text = ""

     return
}

0 个答案:

没有答案