How do you fix Fatal error: index out of range? Swift

89 Views Asked by At

I know I have to return some sort of value in my code so it does not keep crashing with this thread error, but I can't figure it out. Please help! Here is the code- `

@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

There are 0 best solutions below