I have a list of ATM, each ATM has a name.
Now I want to check if my textField.text is equal to that name of each ATM. Surely, I can do this with for loop like so:
for atm in atms {
if nameTextField.text == atm.name {
// Do some Stuff
}
}
But the question here is I want another way to archive this.
Any helps would be appreciated, thanks.
UPDATE 1:
@IBAction func addButtonTapped(sender: UIButton) {
if let _ = atms.filter({$0.name == nameTextField.text}).first {
let alert = UIAlertController(title: "This name has been added!", message: nil, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(alert, animated: true, completion: nil)
} else if nameTextField.text != "" && addressTextField.text != "" && latitudeTextField.text != "" && longitudeTextField.text != "" {
saveData()
dismissViewControllerAnimated(true, completion: nil)
} else {
let alert = UIAlertController(title: "Missing Info", message: nil, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
presentViewController(alert, animated: true, completion: nil)
}
}
Work now, thanks for the answer.
names.contains(nameTextField.text)