How to have a textfield scan for all values in an

2019-08-29 23:26发布

问题:

I am relatively new to swift and programming languages of its kind, and I have really been struggling with having a textfield look for ALL values individually in a string array. So here's my code:

var greetingArray = ["Hi", "hi", "hello", "Hello", "hey", "Hey", "sup", "Sup", "What's up", "what's up"]

Above is the array, and below is the textfield's action.

@IBAction func textUltraAction(textout: UITextField) { 
}

If the "textout.text" is any one of the greetings in greetingArray (but not all of them collectively), I want to complete the action. For example, if "sup" or "hi" is inputed in the textfield, i want to complete the action. I do not want to manually have to type out:

greetinArray[0], greetinArray[1], greetinArray[2], etc...

Thanks in advance for any help anyone can give me!

回答1:

Try this:

if contains(greetingArray, yourTextField.text) {
    //Here you got it
}

Hope this helps.. :)