在另一个类中时,Swift Firebase身份验证不起作用 [英] Swift Firebase authentication not working when in another class
问题描述
我正在尝试一个仅出于娱乐目的的项目,我想使用Firebase.我为用户创建了一个类,用于与用户相关的事情.我正在测试注册功能,当它在一个类中时,它首先产生false,然后产生true.但是,当我不将其放置在单独的类中并直接将其放置在视图控制器中时,它会产生true并按照指示执行segue.由于我不知道如何最好地解释这一点,因此我将在下面显示哪些代码有效,哪些无效.
I am attempting a project just for fun, I would like to use Firebase. I made a class to user for user related things. I was testing the signing up function and when it is within a class it first yields false then yields true. But when I do not place this in a separate class and place it directly in the view controller it yields true and performs the segue as indicated. Due to I not knowing how to explain this best I will show below which code works and which does not.
Firebase嵌入在一个类中:
Firebase embedded in a class:
import Foundation
import Firebase
class Account {
var email: String
var password: String
init(email: String, password: String) {
self.email = email
self.password = password
}
func createAccount ()-> Bool{
var made = false
Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
if (error == nil) {
print("Registration Successful")
made = true
}else{
print(error!)
made = false
}
}
print("this is\(made)")
return made
}
按钮代码:
@IBAction func signUpButton(_ sender: UIButton) {
signUpEmail = emailTextField.text!
signUpPassword = passwordTextField.text!
let signUp = Account(email: signUpEmail, password: signUpPassword);
let signUpOccur = signUp.createAccount()
if( signUpOccur == true){
performSegue(withIdentifier: "signUpToHome", sender: self)
}else{
print(signUpOccur)
}}
////this产生:"this is false",然后"Registration Success",然后"this is true",但是由于首先产生false,所以我不执行Segue
////this yields: "this is false" then "Registration Successful" then "this is true" but does not perform the Segue due to first yielding false I assume
可以正常工作的代码:
signUpEmail = emailTextField.text!
signUpPassword = passwordTextField.text!
Auth.auth().createUser(withEmail: signUpEmail, password: signUpPassword) { (user, error) in
if (error == nil) {
print("Registration Successful")
self.performSegue(withIdentifier: "signUpTohome", sender: self)
}else{
print(error!)
}
}
是否可以将事物放置在类和函数中,还是最好以其工作方式将其放置在按钮中?还是我做错了什么?
Is it possible to place thing within a class and function or would it be best to just place it within the button the way it works? Or is there something I am doing wrong?
感谢您的所有反馈
推荐答案
您的问题是createUser
方法是异步的.您要在createUser
完成之前从函数中返回made
.
Your issue is that the createUser
method is asynchronous. You are returning made
from your function before createUser
finishes.
func createAccount()-> Bool {...}
应该是
func createAccount(completion: (Bool)->Void) {...}
然后,当对createUser的Firebase Auth调用完成时,您调用completion(made)
.
And then when the Firebase Auth call to createUser completes, you call completion(made)
.
您将需要快速阅读编程和闭包中的异步调用,以更好地理解.您可以从本文 a>.
You will need to read up on asynchronous calls in programming and closures in swift to understand better. You can start with something like this article.
这篇关于在另一个类中时,Swift Firebase身份验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!