Auth.auth().createUser(withEmail: email, password: password) { (authResult, error) in
// ...
guard let user = authResult?.user else { return }
}
我从Firebase复制并粘贴了该行,并被这些错误所困扰。
答案 0 :(得分:3)
这些错误是由于您的电子邮件和密码是可选的(字符串与字符串)引起的。您需要先将它们拆开,然后再将它们传递到createUser
中。您可以在进行调用以删除错误之前添加行guard let email = email, let password = password else { return }
。然后根据获取值的方式,如果它们为null,则可能需要显示一条用户消息。
答案 1 :(得分:0)
@耶利米是正确的。您正在尝试将“可选”字符串传递给Firebase函数。
可选含义,它可以具有值,也可以是 nil 。您可以通过两种不同的方式打开这两个字符串(电子邮件和密码):
guard let email = email?.trimmingCharacters(in: .whitespacesAndNewlines).lowercased(),
!emailAddress.isEmpty else {
// Uh oh email address is empty! Show an error.
return
}
guard let password = password, !password.isEmpty else {
// Uh oh password is empty! Show an error.
return
}
login(with: emailAddress, password: password)
或:
if let email = email?.trimmingCharacters(in: .whitespacesAndNewlines).lowercased(),
let password = password, !email.isEmpty, !password.isEmpty {
// Both email and password have values, do stuff:
login(with: emailAddress, password: password)
return
} else {
// Either email or password are empty, show an error
return
}
/// Asks Firebase to login our user.
private func login(with email: String, password: String) {
Auth.auth().signIn(withEmail: email, password: password) { (user, error) in
if let error = error, !error.isEmpty {
// Error signing in!
}
}
}