gpt4 book ai didi

ios - Swift:我无法将 Firebase FIRAuth 函数中的变量传回父函数中声明的变量

转载 作者:行者123 更新时间:2023-11-28 06:39:40 24 4
gpt4 key购买 nike

我是 swift 的新手,我一直在努力学习自己。我遇到了一个问题,我不确定如何解决它。代码如下:

import Foundation
import Firebase

class UserLogin {

var email:String?
var password:String?

init(email:String, password:String){
self.email = email
self.password = password
}

func userLogin() -> String{
var errorMsg:String = ""
//Email & Password Integrity check
if (email == ""){
errorMsg = "Please enter your email"
} else if (email?.rangeOfString("@") == nil || email?.rangeOfString(".") == nil){
errorMsg = "Email is invalid"
}else if (password == ""){
errorMsg = "Please enter your password"
} else if (password?.characters.count < 8){
errorMsg = "Password is invalid"
}else{
print("Logging In... with Email:\(email!) and Password:\(password!)")
//Firebase Authentication Process"

FIRAuth.auth()?.signInWithEmail(email!, password: password!){ (user, error) in
// ...
if (error != nil){
let errorCode = error!.code
if (errorCode == 17009){
errorMsg = "You have entered the wrong password"
} else if (errorCode == 17011){
errorMsg = "Your email does not exist"
} else if (errorCode == 17010) {
errorMsg = "You have tried to login too many times with the wrong credentials. Please try again later."
} else {
print(error)
}
} else {
print("User is Logged In")
errorMsg = "You have successfully Logged In"
}
}
}
return errorMsg
}

}

基本上在我的 ViewController 中,我有一个像这样工作的单独代码

let alert = UIAlertController(title: "Error", message: errorMsg, preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default, handler: nil)
alert.addAction(action)
self.presentViewController(alert, animated: true, completion: nil)

这对我的 userLogin 函数中的所有 errorMsg 都适用,但对于根据 FIRAuth 提供的错误代码生成的少数 errorMsg,它不会出现。

我仔细阅读并认为这可能是因为 FIRAuth 是一个异步调用,但我不知道如何解决它。

抱歉,如果这听起来真的很愚蠢,但我一整天都在想办法,但无济于事,如果能从你们那里得到一些帮助,那就太棒了。

添加:我按照建议实现了 CompletionHandler,但我不明白为什么它不起作用,尽管它应该...以下是我的代码。

用户登录1.swift

import Foundation
import Firebase

class UserLogin1 {

var email:String?
var password:String?

init(email:String, password:String){

self.email = email
self.password = password
}

func userLogin(completion:(message:String)->()) {

var errorMsg:String = ""
//Email & Password Integrity check
if (email == ""){

errorMsg = "Please enter your email"

} else if (email?.rangeOfString("@") == nil || email?.rangeOfString(".") == nil){

errorMsg = "Email is invalid"

}else if (password == ""){

errorMsg = "Please enter your password"

} else if (password?.characters.count < 8){

errorMsg = "Password is invalid"

}else if (errorMsg != ""){

completion(message: errorMsg)

}else{

print("Logging In... with Email:\(email!) and Password:\(password!)")
//Firebase Authentication Process"

FIRAuth.auth()?.signInWithEmail(email!, password: password!){ (user, error) in
// ...
if (error != nil){

let errorCode = error!.code

if (errorCode == 17009){

errorMsg = "You have entered the wrong password"

} else if (errorCode == 17011){

errorMsg = "Your email does not exist"

} else if (errorCode == 17010) {

errorMsg = "You have tried to login too many times with the wrong credentials. Please try again later."

} else {

print(error)

}

} else {
print("User is Logged In")
errorMsg = "You have successfully Logged In"
}


}
completion(message: errorMsg)

}

}

}

登录 View Controller

import UIKit

class LoginViewController: UIViewController {

//Properties
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!

//Actions
@IBAction func loginButton(sender: AnyObject) {

let email = self.emailTextField.text!
let password = self.passwordTextField.text!

let user = UserLogin1(email: email, password: password)

user.userLogin(){ (message:String) in
print(message)
}


}

最佳答案

根据我的评论,我提到您将有两种可能的解决方案。最佳选择将取决于您要实现的目标。但我相信你可以让它与它们中的任何一个一起工作。

Completion Handler

func userLogin(completion:(message:String)->()){
var errorMsg:String = ""
if (email == ""){
...
//check if found any errors yet
}else if (errorMsg != ""){
completion(errorMsg)
} else {
FIRAuth.auth()?.signInWithEmail(email!, password: password!){ (user, error) in
if (error != nil){
...
} else {
errorMsg = "You have successfully Logged In"
}
completion(errorMsg)
}
}
}

userLogin(){ (message:String) in
// this will only be called when userLogin trigger completion(errorMsg)...
print(message)
}

与自己

func userLogin() -> Void{
var errorMsg:String = ""
if (email == ""){
...
//check if found any error yet
}else if (errorMsg != ""){
self.errorMsg = errorMsg
} else {
FIRAuth.auth()?.signInWithEmail(email!, password: password!){ (user, error) in
if (error != nil){
...
} else {
errorMsg = "You have successfully Logged In"
}
self.errorMsg = errorMsg
}
}
}

关于ios - Swift:我无法将 Firebase FIRAuth 函数中的变量传回父函数中声明的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38293785/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com