arraysbuttonlabelarc4random

How do you print a random result with user input in labels?


With the click of a button trying to randomly show one out of the two labels that have been entered with data. I am now able to get the first set of code working 1 out of 2 Labels randomly picked. However, on the second half of code when 3 labels hold value it is now printing 2 out of 3. I only want one result for both methods. Please help! Here is my code so far-

              //**UPDATE**                                                                         
@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 && valueLbl3.text?.isEmpty == true
        {



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


             pickTopic = Int(arc4random_uniform(UInt32(topics.count-0)))
            topics.remove(at: pickTopic)

            resultLbl.text = "\(topics)"

            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)"
       // resetting variable value
     valueLbl1.text = ""
    valueLbl2.text = ""
     valueLbl3.text = ""

     return
    }

Solution

  • Figured it out for those who want to know.

     @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 && valueLbl2.text?.isEmpty == true
            {
                let topics = [valueLbl1.text!, valueLbl2.text!, valueLbl3.text!]
    
               let pickTopic = topics[Int(arc4random_uniform(UInt32(topics.count)))]
    
                resultLbl.text = "\(pickTopic)"
                valueLbl1.text = ""
                valueLbl2.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
    
          {
            let topics = [ valueLbl1.text!, valueLbl2.text!, valueLbl3.text!]
    
    
       let  pickTopic = topics[Int(arc4random_uniform(UInt32(topics.count)))]
    
    
        resultLbl.text = "\(pickTopic)"
        // resetting variable value
        valueLbl1.text = ""
        valueLbl2.text = ""
        valueLbl3.text = ""
    
       return()
    
    
        }