单击“注册”按钮后,该应用程序应运行一个功能,在其中检查用户输入的用户名是否已被使用。如果是这样,它将停止“创建帐户”过程,告诉用户输入另一个用户名。
但是,输入相同的用户名并单击“注册”按钮后,它使我的应用程序崩溃,并显示Thread 1: signal SIGABRT
错误。错误行注释如下。
如果有人可以帮助我,将不胜感激。
数据库JSON文件:
{
"usernames" : {
"itsandyjiang" : "0vFQYnfIbahHdC1RsuNMW45HYNR2"
},
"users" : {
"0vFQYnfIbahHdC1RsuNMW45HYNR2" : {
"email" : "andyjiang55@yahoo.com",
"username" : "itsandyjiang"
}
}
}
检查用户是否输入了未使用的用户名的功能:
func isValidUsername(username: String) {
let usernameRegex = ".{3,}"
var valid = NSPredicate(format: "SELF MATCHES %@", usernameRegex).evaluate(with: username)
if valid {
valid = !username.contains("Invalid username id")
}
if valid == false {
usernameLabel.textColor = UIColor.red
usernameLabel.text = "USERNAME MUST BE AT LEAST 3 CHARS"
usernameTextField.layer.addBorder(edge: UIRectEdge.bottom, color: UIColor.red, thickness: 1.5)
} else {
let ref = Database.database().reference(fromURL: "https://testapp-testtest.firebaseio.com/")
let usernamesRef = ref.child("usernames")
usernamesRef.observeSingleEvent(of: .value, with: { (snapshot) in
let usernamesArray = Array(snapshot.value as! [String]) //ERROR LINE
for storedUsername in usernamesArray {
if storedUsername == self.usernameTextField.text! {
self.usernameVerified = false
self.usernameLabel.textColor = UIColor.red
self.usernameLabel.text = "USERNAME TAKEN"
self.usernameTextField.layer.addBorder(edge: UIRectEdge.bottom, color: UIColor.red, thickness: 1.5)
return
}
}
self.usernameVerified = true
self.usernameTextField.layer.addBorder(edge: UIRectEdge.bottom, color: UIColor.black, thickness: 1.5)
self.usernameLabel.textColor = UIColor.black
self.usernameLabel.text = "USERNAME"
}) { (error) in
print(error.localizedDescription)
}
}
}
单击“登录”按钮后运行的功能:
@IBAction func onSignUp(_ sender: Any) {
isValidUsername(username: usernameTextField.text!)
isValidEmail(email: emailTextField.text!)
isValidPassword(password: passwordTextField.text!)
if passwordVerified && emailVerified && usernameVerified {
Auth.auth().createUser(withEmail: emailTextField.text!, password: passwordTextField.text!) { (authResult, error) in
if error != nil {
self.errorLabel.alpha = 1
self.errorLabel.text = error?.localizedDescription
}
guard let user = authResult?.user else {
return
}
//Successfully Authenticated User
let ref = Database.database().reference(fromURL: "https://testapp-testtest.firebaseio.com/")
let usersReference = ref.child("users").child(user.uid)
let values = ["username": self.usernameTextField.text, "email": self.emailTextField.text]
let usernamesReference = ref.child("usernames")
let values2 = [self.usernameTextField.text!: user.uid]
usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
if err != nil {
print(err!)
return
}
//Successfully registered user's data to database
usernamesReference.updateChildValues(values2, withCompletionBlock: { (err, ref) in
if err != nil {
print(err!)
return
}
self.errorLabel.alpha = 0
})
})
}
} else {
errorLabel.alpha = 1
print("Password/Email/Username verification not complete!")
}
}
答案 0 :(得分:0)
您需要将其转换为字典,因为所附的json暗示了
let con = snapshot.value as! [String:Any]
let usernamesArray = Array(con.keys)
您当前的方案中也有重复项,因此请考虑将所有用户数据设置在1个表中,不要将它们分开,也许仅仅是这样
{
"users" : {
"0vFQYnfIbahHdC1RsuNMW45HYNR2" : {
"email" : "andyjiang55@yahoo.com",
"username" : "itsandyjiang"
}
}
}
然后
let ref = Database.database().reference(fromURL: "https://testapp-testtest.firebaseio.com/")
let usernamesRef = ref.child("users")
usernamesRef.observeSingleEvent(of: .value, with: { (snapshot) in
let con = snapshot.value as! [String:[String:Any]]
var usernamesArray = [String]()
Array(con.keys).forEach {
if let res = con[$0] , let username = res["username"] as? String {
usernamesArray.append(username)
}
}
// after that check whether exists / not
---