我有一份ATM清单,每个ATM都有一个名字。
现在我想检查我的textField.text是否等于每个ATM的名称。当然,我可以这样做for循环:
for atm in atms {
if nameTextField.text == atm.name {
// Do some Stuff
}
}
但问题是我想要另一种方式来存档。
任何帮助都将不胜感激,谢谢。
更新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)
}
}
立即开始工作,谢谢你的回答。
答案 0 :(得分:4)
你可以这样做:
if let atm = atms.filter({$0.name == nameTextField.text}).first {
//Show alert
let alert = UIAlertController(title: "This name has been added!", message: nil, preferredStyle: .Alert)
let action = UIAlertAction(title: "Ok", style: .Default, handler: nil)
alert.addAction(action)
presentViewController(alert, animated: true, completion: nil)
} else {
//do another stuff
}
它将检查匹配的第一个atm