Swift 无法分配给值:“user”是“let”常量-已修复
我正在尝试创建一个登录页面,其中用户使用身份验证电子邮件/密码登录,如果他们被声明为管理员或用户,performsgue将进入另一个页面 这是我的密码:Swift 无法分配给值:“user”是“let”常量-已修复,swift,Swift,我正在尝试创建一个登录页面,其中用户使用身份验证电子邮件/密码登录,如果他们被声明为管理员或用户,performsgue将进入另一个页面 这是我的密码: @IBAction func submit(_ sender: UIButton) { if let email = txtemail.text, let passowrd = txtpass.text { Auth.auth().signIn(withEmail: email, password: passowrd)
@IBAction func submit(_ sender: UIButton) {
if let email = txtemail.text, let passowrd = txtpass.text {
Auth.auth().signIn(withEmail: email, password: passowrd) { (user, error) in
Database.database().reference().child("user").child("admin")
if user = true {
self.performSegue(withIdentifier: "adminlogin", sender : self)
}
if user = false {
self.performSegue(withIdentifier: "login", sender : self)
}
}
}
{ Auth.auth().signIn(withEmail: email, password: passowrd) { (user, error) in
if user != nil {
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("user").child(uid!).child("admin").observeSingleEvent(of: .value, with: { (snapshot) in
// Checking if user is found
if snapshot.exists() {
// admin is found
self.performSegue(withIdentifier: "adminlogin", sender : self)
} else {
// admin is not found going to user login
self.performSegue(withIdentifier: "login", sender : self)
}
})
但是,不断出现此错误:
无法分配给值:“user”是“let”常量
答复:
我通过对代码执行以下操作修复了此问题:
@IBAction func submit(_ sender: UIButton) {
if let email = txtemail.text, let passowrd = txtpass.text {
Auth.auth().signIn(withEmail: email, password: passowrd) { (user, error) in
Database.database().reference().child("user").child("admin")
if user = true {
self.performSegue(withIdentifier: "adminlogin", sender : self)
}
if user = false {
self.performSegue(withIdentifier: "login", sender : self)
}
}
}
{ Auth.auth().signIn(withEmail: email, password: passowrd) { (user, error) in
if user != nil {
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("user").child(uid!).child("admin").observeSingleEvent(of: .value, with: { (snapshot) in
// Checking if user is found
if snapshot.exists() {
// admin is found
self.performSegue(withIdentifier: "adminlogin", sender : self)
} else {
// admin is not found going to user login
self.performSegue(withIdentifier: "login", sender : self)
}
})
现在,当我使用Admin登录时,我会转到另一个视图控制器,而不是使用用户登录 如果user=true,则将single=作为赋值。要检查值是否等于某个值,请使用user==true=作为赋值运算符
==是比较运算符
您希望使用==进行比较
用户和错误对象是不可变的,这意味着您不能更改它们。但它们保存着您想要检查的值
我已更新您的代码以使其正常工作:
@IBAction func submit(_ sender: UIButton) {
if let email = txtemail.text, let passowrd = txtpass.text
{
Auth.auth().signIn(withEmail: email, password: passowrd) { (user, error) in
Database.database().reference().child("user").child("admin")
if user == true {
self.performSegue(withIdentifier: "adminlogin", sender : self)
}
if user == false {
self.performSegue(withIdentifier: "login", sender : self)
}
}
}
}
您无法通过将该用户与true或false进行比较来检查该用户是否为管理员。我建议用户登录,然后通过so验证UID是否为管理员:
let databaseRef = Database.database().reference(withPath: "user/admin/(UID after logged in)")
databaseRef.observeSingleEvent(of: .value, with: { (snapshot) in
// Checking if user is found
if snapshot.exists() {
// admin is found
self.performSegue(withIdentifier: "adminlogin", sender : self)
} else {
// admin is not found going to user login
self.performSegue(withIdentifier: "login", sender : self)
}
})
您只需通过检查条件{…}或{}来实现这一点。另外,用户需要一个更好的名称。对用户来说是真是假意味着什么?更好的方法是USERISAdministration获取错误二进制运算符“==”不能应用于“AuthDataResult”和“Bool”类型的操作数,因此user不是Bool?抱歉,我对swift非常陌生。我不这么认为,但我认为应该是,因为它要么是真的,要么是假的。只有当用户本身是一个傻瓜时,你才能测试某个东西是真是假。如果用户不是Bool,则不能。您是否正在尝试确定用户是否存在,即不是nil?获取错误二进制运算符“==”无法应用于“AuthDataResult”和“Bool”类型的操作数。请您详细解释一下。至于我应该把这个代码放在实际登录页面的什么地方?