忽然笑
enter code here导入UIKit导入CoreDataclass ViewController:UIViewController {@IBOutlet weak var lgnusername: UITextField!@IBOutlet weak var lgnpassword: UITextField!var result = NSArray()override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib.}@IBAction func loginaction(_ sender: Any){ let app = UIApplication.shared.delegate as! AppDelegate let context = app.persistentContainer.viewContext let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Login") let searchString = self.lgnusername.text let searcghstring2 = self.lgnpassword.text request.predicate = NSPredicate (format: "username == %@", searchString!) do { let result = try context.fetch(request) if result.count > 0 { let n = (result[0] as AnyObject).value(forKey: "username") as! String let p = (result[0] as AnyObject).value(forKey: "userpassword") as! String print("p,n",n,p) if (searchString == n && searcghstring2 == p) { let UserDetailsVc = self.storyboard?.instantiateViewController(withIdentifier: "userViewController") as! userViewController //UserDetailsVc.myStringValue = lgnusername.text self.navigationController?.pushViewController(UserDetailsVc, animated: true) } else if (searchString == n || searcghstring2 == p) { // print("password incorrect ") let alertController1 = UIAlertController (title: "no user found ", message: "password incorrect ", preferredStyle: UIAlertControllerStyle.alert) alertController1.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) present(alertController1, animated: true, completion: nil) } } else { let alertController1 = UIAlertController (title: "no user found ", message: "invalid username ", preferredStyle: UIAlertControllerStyle.alert) alertController1.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) present(alertController1, animated: true, completion: nil) print("no user found") } } catch { print("error") }}}