Swift 2 - so, I have an array and a uitextfield that a user inputs a string, I want to check whether the textfield.text is equal to ANY of the values in the array, can I do this with one line of code rather than lots of if's and else if's?!?
Asked
Active
Viewed 84 times
1 Answers
1
This is a generic code that will do what you are looking for. The if statement checks to see if a given value is equal to something that is located in the array. Simply replace the arr.contains()
with the output you have given for your UITextfield.text Try to do a little research before you post. I can see that you are new here, so here is a little bit of help.
var arr = [1,2,3,4]
if arr.contains(3) {
//do something
}

Caleb Bach
- 162
- 11
-
Okay so I understand that but it doesn't really achieve what I want, or at least I can't see it achieving what I want (I'm new to coding) I have an array of around 50 postcodes, a user will input their postcode into a textfield and if their postcode is in the array I want to perform a segue if not show an error label!! – Dean Nov 12 '15 at 19:49
-
Provide what you have tried and I will point you in the correct direction. – Caleb Bach Nov 12 '15 at 19:50
-
I can do a bit later, I currently don't have access to my laptop!! – Dean Nov 12 '15 at 19:52
-
So I had a moment of clarity and sorted it, I was totally over complicating it, oops!! I can see that it probably looks like a silly question now :/ however, thanks for your guidance!! – Dean Nov 12 '15 at 20:58