在 Swift 中使用 for 循环完成
Completion with for-loop in Swift
我正在尝试使用 Firebase 编写一个函数,它允许知道用户名是否被占用。
for username in dict.values {
if username as? String == self.usernameText.text! {
appDelegate.visualReturnBottom(message: "Username already taken.", color: brandBlue, backgroundColor: UIColor.white)
return
}
}
问题是,如果没有使用用户名,我想更改视图控制器。为此,我必须等待循环结束,我真的不知道该怎么做。
我想添加这部分代码"if the loop is over...:"
let nextVC = self.storyboard?.instantiateViewController(withIdentifier: "InscriptionSecondVC") as! Inscription2TableViewController
self.navigationController?.pushViewController(nextVC, animated: true)
您可能希望充分利用 Firebase 的功能并使您的数据库结构有所不同,以帮助您更高效地完成这项工作。
您可能想尝试创建一个用户名节点并像这样构造它。当用户使用用户名注册时,您将他们的用户名添加为 KEY,将 VALUE 添加为 1。这样您就可以简单地检查用户名树是否包含特定用户名:
usernames:
- Bob: 1
- Dan: 1
- Billy: 1
这样您就可以简单地检查用户名树是否具有 usernameText.text 输出的子节点,如下所示:
func handleCheckUsername() {
guard let username = self.usernameText.text else { return }
let reference = Database.database().reference()
reference.child("usernames").observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild(username) {
print("Username is taken.")
} else {
print("Username isn't taken.")
}
}, withCancel: nil)
}
如果您正在检查用户名匹配值的字典,则必须等到循环完成。没有其他方法可以执行该检查。由于您的代码目前已构成,这应该可以工作。
for username in dict.values {
if username as? String == self.usernameText.text! {
appDelegate.visualReturnBottom(message: "Username already taken.", color: brandBlue, backgroundColor: UIColor.white)
return
}
}
// if the username is already taken, you will never reach this point.
let nextVC = self.storyboard?.instantiateViewController(withIdentifier: "InscriptionSecondVC") as! Inscription2TableViewController
self.navigationController?.pushViewController(nextVC, animated: true)
至于,我同意,你应该考虑重组你的数据。这只是对您的具体问题的回答。
我正在尝试使用 Firebase 编写一个函数,它允许知道用户名是否被占用。
for username in dict.values {
if username as? String == self.usernameText.text! {
appDelegate.visualReturnBottom(message: "Username already taken.", color: brandBlue, backgroundColor: UIColor.white)
return
}
}
问题是,如果没有使用用户名,我想更改视图控制器。为此,我必须等待循环结束,我真的不知道该怎么做。
我想添加这部分代码"if the loop is over...:"
let nextVC = self.storyboard?.instantiateViewController(withIdentifier: "InscriptionSecondVC") as! Inscription2TableViewController
self.navigationController?.pushViewController(nextVC, animated: true)
您可能希望充分利用 Firebase 的功能并使您的数据库结构有所不同,以帮助您更高效地完成这项工作。
您可能想尝试创建一个用户名节点并像这样构造它。当用户使用用户名注册时,您将他们的用户名添加为 KEY,将 VALUE 添加为 1。这样您就可以简单地检查用户名树是否包含特定用户名:
usernames:
- Bob: 1
- Dan: 1
- Billy: 1
这样您就可以简单地检查用户名树是否具有 usernameText.text 输出的子节点,如下所示:
func handleCheckUsername() {
guard let username = self.usernameText.text else { return }
let reference = Database.database().reference()
reference.child("usernames").observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild(username) {
print("Username is taken.")
} else {
print("Username isn't taken.")
}
}, withCancel: nil)
}
如果您正在检查用户名匹配值的字典,则必须等到循环完成。没有其他方法可以执行该检查。由于您的代码目前已构成,这应该可以工作。
for username in dict.values {
if username as? String == self.usernameText.text! {
appDelegate.visualReturnBottom(message: "Username already taken.", color: brandBlue, backgroundColor: UIColor.white)
return
}
}
// if the username is already taken, you will never reach this point.
let nextVC = self.storyboard?.instantiateViewController(withIdentifier: "InscriptionSecondVC") as! Inscription2TableViewController
self.navigationController?.pushViewController(nextVC, animated: true)
至于