gpt4 book ai didi

swift - 函数不会调用 Swift 3

转载 作者:行者123 更新时间:2023-11-28 15:27:18 25 4
gpt4 key购买 nike

@IBAction func signup(_ sender: Any) {

print("began signup process")

guard let fullname = fullnameField.text, fullname != "" else {
print("FULLNAME field is empty")
return
}

guard let username = usernameField.text, username != "" else {
print("USERNAME field is empty")
return
}

guard let email = emailField.text, email != "" else {
print("EMAIL field is empty")
return
}

guard let password = passwordField.text, password != "" else {
print("PASSWORD field is empty")
return
}

print("all fields good")

mainActivityIndicator.startAnimating()

self.checkUsernameAvailability(username: username, completion: {
result in
print("starting check")
...
})


print("finished the function")
}

问题基本上是不会调用 checkUsernameAvailability 函数内部的任何内容。控制台看起来像这样:

began signup process
all fields good
finished the function

它不会在函数内部打印“开始检查”或运行任何代码。这可能是一个菜鸟错误,如果这是一个愚蠢的问题,我很抱歉。 future 的感谢。

P.S 我检查了整个控制台,没有与此相关的错误。

编辑:这是函数内部的代码

func checkUsernameAvailability(username: String, completion: @escaping (Bool) -> Void) {
_ = Database.database().reference().child("usernames").observe(.childAdded, with: {
snapshot in

print("checking username availability")
print(snapshot)

completion(true)

let dict = snapshot.value as? [String: AnyObject]
for handled in (dict?.values)! {

print("stumbled")
print(handled)
if username.lowercased() == handled.lowercased {
completion(false)
}
}
})
}

还有……

self.checkUsernameAvailability(username: username, completion: {
result in

print("starting check")

if result == false {
print("username is taken")
self.mainActivityIndicator.stopAnimating()
return
} else {

Auth.auth().createUser(withEmail: email, password: password, completion: {
(user, error) in

if error != nil {
print(error ?? "ERROR OCCURED")
self.mainActivityIndicator.stopAnimating()
return
}

let ref = Database.database().reference()
ref.child("~/users/\(user?.uid ?? "0")/username").setValue(username.lowercased())
ref.child("~/users/\(user?.uid ?? "0")/fullname").setValue(fullname)
ref.child("usernames/\(user?.uid ?? "0")").setValue(username.lowercased())

self.mainActivityIndicator.stopAnimating()
})

}
})

最佳答案

Database.database().reference().child("usernames").observe 调用根本不会调用 with block 。我会假设在观察到事件 .childAdded 时调用该 block ,但我看不到可以添加子项的代码。在我看来,这就像您正在设置一个异步观察器,因此该代码不会在您进行调用时运行,它会在受监视事件发生时运行。

关于swift - 函数不会调用 Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45075172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com