How can I check if the text of a button contains an element of an array?

Ryan Powell

I'm pretty new to this so bear with me. What I'm trying to do is check if the button is currently displaying an element from an array; array Answers. What would be the best route to do so?

@IBAction func qButton1(_ sender: Any) {
    if (sender as AnyObject).currentTitle == (Answer in Answers){
        PickQuestion()

    }
    else{
        missedWords.append(quizLabel.text!)

    }
}
Duncan C

You should not use the button's title to store data and compare it to a value.

Any time you write code who's logic depends on text displayed to the user you are paining yourself into a corner in terms of localization.

Using a numeric tag to index into an array of strings would be a much better idea.

Say you create an array of title strings.

Make each button's tag be 100 + the array index.

let startingTag = 100

@IBAction func qButton1(_ sender: UIButton) {
    guard let index = sender.tag where startingTag >= 100 else {
      return
    }
    //fetch your string from the array of button title strings you set up.
    let stringToFind = strings[index]
    if answers.contains(where: {$0 == stringToFind}) {
      //Found the current' buttons' string in answers
    } else {
      //string not found
    }

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I check if an ArrayList contains any element of another ArrayList?

How can I check if an element of an iterable contains something

How can I check if javascript array already contains a specific array

How can I find and click button element in cypress by the text on the button

How to check if array of array contains element

How can I check if a Perl array contains a particular value?

How can I check if sentences in array contains exact words in Swift?

How can I check if an array contains null values inside typescript?

How can I check if a array contains multiple elements?

how to click a button if another element contains a text

How can I check if a string contains characters stored in an array but return false if it contains any characters not in the array

How to check if a float array contains an exponential element

How do I check if a text variable in an array contains the most amount of text from every value of another array?

How can I use Cypress to check the text of an element's event?

How can I check and save text from span element

How to check if an html element contains it's own text (contains textNode)

How can I change the text of a button which is in class element?

How to check array if it contains a string with same text?

How can I check the array of radio name button if it is not checked

How can I check if an element in an array is equal to something in C?

How can I check if an element exists in array of objects using lodash

How can one check if an array contains a substring?

How can I create an XPath that will look for button text or text from a nested element within the button?

How can I check if a polygon contains a point?

JavaScript: How can i check if an array contains elements in common with a second array?

how can i check if an array of objects contains a key which is in array of strings js?

In Python, how can I check if an array contains all elements of another array/list, including duplicates?

How can I initialize "array a" that contains "array b" that contains "array a"

How can I use Hamcrest to check if each element in an array of doubles is "close" to each element in another array?