【问题标题】:to look at the suffix of the text of the text field查看文本字段的文本后缀
【发布时间】:2019-09-15 14:04:12
【问题描述】:

我想让人们登录到某个域,但它总是显示错误。

无法将“String”类型的值转换为预期的参数类型“Int”)。

我该如何处理?

import UIKit
import FirebaseAuth

class ViewController: UIViewController {

@IBOutlet var emailTextField: UITextField!
@IBOutlet var passwordTextField: UITextField!
@IBAction func signUp(_ sender: Any) {




    Auth.auth().createUser(withEmail: emailTextField.text!, password: passwordTextField.text!) { (result, error) in
        if error != nil{
            print(error)
        }else{
            self.performSegue(withIdentifier: "createdUser", sender: self)
        }
    }
}
@IBAction func signIn(_ sender: Any) {

    Auth.auth().signIn(withEmail: emailTextField.text!, password: passwordTextField.text!) { (result, error) in
        if error != nil{
            print(error)
        }else{
            var hi = self.emailTextField.text

            if hi?.suffix("@hkugac.edu.hk"){ //error here


                self.performSegue(withIdentifier: "logged", sender: self)
        }


            }

        }
    }



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}


}

【问题讨论】:

    标签: xcode xcode10 suffix


    【解决方案1】:

    尝试使用 hasSuffix 方法。 https://developer.apple.com/documentation/swift/string/1541149-hassuffix

    var hi = self.emailTextField.text
    
    if let mail = hi, mail.haSuffix("@hkugac.edu.hk")
    {
        self.performSegue(withIdentifier: "logged", sender: self)
    }
    else
    {
        // Dome some error handling here.
    }
    

    另外,我认为在调用signIn 函数之前检查邮件会更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-20
      • 2021-10-09
      相关资源
      最近更新 更多