Я пишу код для регистрации и регистрации пользователя. он должен проверить, является ли текстовое поле нулевым, прежде чем выполнять больше кода.
guard let a = b else {/ * код, когда b равно nil * / return}
Я знаю, что оператор guard должен возвращаться, если b - ноль. В моем коде b - ноль, но он не вернулся, не знаю почему. Также я распечатываю содержимое, которое мне кажется ноль. Спасибо
Вот кодировка
класс userLoginView: UIViewController {
let realm = try! Realm()
var email:String?
var password:String?
var userResult:Results<userInfo>?
var logedin:Bool = false
@IBOutlet weak var emailtextfile: UITextField!
@IBOutlet weak var passwordtextfile: UITextField!
@IBOutlet weak var errorMessage: UILabel!
@IBOutlet weak var fnametext: UITextField!
@IBOutlet weak var lnametext: UITextField!
@IBOutlet weak var emailsigntext: UITextField!
@IBOutlet weak var signpasstext: UITextField!
@IBOutlet weak var passagaintext: UITextField!
@IBOutlet weak var contractortext: UITextField? = nil
@IBOutlet weak var signError: UILabel!
@IBAction func loginButton(_ sender: Any) {
guard email == emailtextfile!.text,
password == passwordtextfile!.text else{
errorMessage?.text = "Please fill in your email and password"
return
}
if checkUser(_email: email ?? "", _pass: password ?? "" ){
logedin = true
if let adminView = self.storyboard?.instantiateViewController(withIdentifier: "adminView") as? adminviewController{
self.navigationController?.pushViewController(adminView, animated: true)
UserDefaults.standard.set(email, forKey: "email")
UserDefaults.standard.set(password, forKey: "password")
}
}else{
errorMessage?.text = "Email or Password doesn't match our record"
}
}
@IBAction func signupsegues(_ sender: UIButton!) {
if let signupview = storyboard?.instantiateViewController(withIdentifier: "signupView"){
navigationController?.pushViewController(signupview, animated: true)
}
}
@IBAction func signmeup(_ sender: UIButton!) {
let contractor = contractortext?.text ?? ""
guard let email = emailsigntext.text else{
signError?.text = "Please fill in email"
return}
guard let fname = fnametext.text else{
signError?.text = "Please fill in first name"
return}
guard let lname = lnametext.text else{
signError?.text = "Please fill in last name"
return}
guard let pass = signpasstext.text else{
signError?.text = "Please fill password"
return}
guard let passagain = passagaintext.text else{
signError?.text = "Please fill confirm password"
return
}
do{
print("pass:" + pass)
print("passagain" + passagain)
print("email:" + email)
print("fname:" + fname)
print("lname:" + lname)
try signupUser(_email: email, _firstname: fname, _lastName: lname, _password: pass, _comfirmpass: passagain, _contractor: contractor)
}catch{
print("some error")
}
}
@IBAction func forgotButton(_ sender: Any) {
}
override func viewDidLoad() {
super.viewDidLoad()
userResult = realm.objects(userInfo.self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func checkUser(_email:String, _pass:String) -> Bool {
var checked:Bool = false
let userrecord = userResult?.filter("userEmail = %@", email!)
let emailrecord = userrecord?.first?.userEmail
let passrecord = userrecord?.first?.userPass
if (email == emailrecord && password == passrecord){
checked = true
}else {checked = false}
return checked
}
func signupUser(_email:String!,_firstname:String!,_lastName:String!,_password:String!,_comfirmpass:String!,_contractor:String?) throws ->Void {
if _password.elementsEqual(_comfirmpass) {
let temp = userInfo(value: [_email!,_password!,_firstname!,_lastName!,_contractor ?? "",false])
try realm.write {
realm.add(temp)
}
}else{
signError?.text = "password does not match"}
}
}