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?!?
1 Answer
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
}
4 Comments
Dean
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!!
Caleb Bach
Provide what you have tried and I will point you in the correct direction.
Dean
I can do a bit later, I currently don't have access to my laptop!!
Dean
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!!