ios - 为什么在检查 Firebase 中是否存在用户名时从 textField 中删除文本时应用程序崩溃?

标签 ios swift firebase

当用户在注册屏幕上输入新用户名时,userNameTextField 中的文本会根据 Firebase 实时数据库进行检查,以确保用户名尚未被占用。效果很好!

如果您只是在开始输入后删除一两个字母,没问题。但是,如果您在开始输入后删除所有字母,iOS 模拟器就会崩溃。这是代码:

@IBAction func userNameTextFieldEditingChanged(_ sender: Any) {
    Auth.auth().signInAnonymously { (user, error) in

        if let error = error
        {
            self.showMessage(messageToDisplay: error.localizedDescription)
            return
        }

        if user != nil {
            var databaseReference: DatabaseReference!
            databaseReference = Database.database().reference()
            Database.database().reference().child("allUsers").child(self.userNameTextField.text!).observeSingleEvent(of: .value, with: {(usernameSnap) in

                if usernameSnap.exists(){
                    self.userNameStatusLabel.text = "Username is already taken."
                    self.nextButton.isEnabled = false
                    self.nextButton.alpha = 0.25
                } else{
                    self.userNameStatusLabel.text = ""
                }

            })
        }   
    }
}

应用程序崩溃后,它会在 Xcode 中向我显示带有消息的 AppDelegate

"Thread 1: signal SIGABRT" after the line "class AppDelegate: UIResponder, UIApplicationDelegate {"

在下面的调试区域中,消息显示为

"libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)"

知道它崩溃的原因以及如何修复它吗?感谢您的帮助!

最佳答案

看起来你得到了

Must be a non-empty string and not contain '.' '#' '$' '[' or ']'

这意味着在您的情况下,您发送的是带有 firebase 的空字符串,这是不可能的。

关于ios - 为什么在检查 Firebase 中是否存在用户名时从 textField 中删除文本时应用程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54837393/

相关文章:

ios - 如何在 CollectionView 的组合布局中更改选择颜色

ios - 是否有 [NSString stringWithFormat :] for NSAttributedString 的任何类似物

swift - 您的规范源都不包含满足依赖关系的规范

javascript - 火存储 : can't match midnight with date filter in where() clause

iOS 在后台保存主线程 NSManagedObjectContext 更改

ios - ReplayKit分享视频到facebook没有评论

firebase - 如何将 LINE 登录集成到 Firebase Auth 中

ios - 为什么我的 Firebase 查询不返回在控制台中手动创建的对象?

ios - 如何返回方法的输出是使用 NSURLConnection 异步调用 block

ios - 根据所选日期快速从手动 datePicker 获取时间