lundi 27 juillet 2020

Randomly grab string from array, use as placeholder text, and equate to answer string in swift

I have two UITextFields that get the placeHolder.text randomly from an array. The part I am stuck on, is having an element in an array of answers, correspond to the placeHolder text.

Meaning: placeHolder.text is a question, user text input is the answer, and we check that the user answer is the same as the hard coded value.

sample code:

   let questionArray = ["name of your cat?", "name of your other cat?"]
   let questionArray2 = { more stupid questions ]
 
    let answerArray = ["cat damen", "diabetes"]
    let answerArray2 = [ more stupid answers ]

    var answerContainer: String()
    var answerContainer2: String()

    uitextField1.placeHolder = questionArray.randomElement()
    uitextField2.placeHolder = questionArray2.randomElement()
    
    Heres where I get stupid :
    
    func answerGenerator {
       if UItextField1.placeHolder == questionArray[0] {
          answerContainer = answerArray[0]
      }
    }

func submit(_ sender: UIButton) {
       if uitextField.text == answerContainer && uitextField2.text == answerContainer2 { 
   do good stuff 
    }
  }

I tried using a dictionary, but got stuck. I also thought about an Enum / Switch Case, but also got stuck as I have two textFields that need the placeHolder generated randomly.

so what happens is the actual answer that's supposed to be allocated to its question, maybe it happens, but I'm not grabbing it correctly to compare what the user will type in the textfield.

Its so easy, but I'm overthinking this.




Aucun commentaire:

Enregistrer un commentaire