How to have a textfield scan for all values in an array individually in swift?

你。 提交于 2019-12-25 04:13:09

问题


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.. :)



来源:https://stackoverflow.com/questions/27914193/how-to-have-a-textfield-scan-for-all-values-in-an-array-individually-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!