ios - FireBase PhoneAuth 线程 1 : Fatal error: Unexpectedly found nil while unwrapping an Optional value

标签 ios swift

我尝试从 firebase 设置 PhoneAuth,但我是新的,我只是不知道为什么会这样

这是部分代码

 let credential: PhoneAuthCredential = PhoneAuthProvider.provider().credential(withVerificationID: defaults.string(forKey: "AuthVID")!, verificationCode: self.codeInputField.text!)

Here is error

What Xcode shows about error

我认为问题出在这里,但我不知道如何解决

Printing description of credential:(PhoneAuthCredential) credential = <variable not available>

谢谢

最佳答案

问题似乎出在下一行,因为您正在强行解包可选。

let credential: PhoneAuthCredential = PhoneAuthProvider.provider().credential(withVerificationID: defaults.string(forKey: "AuthVID")!, verificationCode: self.codeInputField.text!)

原因

可能是你的 AuthVID 是 nil ,(UserDefault 中没有 String with key AuthID )或者 self.codeInputField 没有连接到 socket

解决方案

提供默认值或使用可选链接。

if let strAuthVID = defaults.string(forKey: "AuthVID"), let code = self.codeInputField.text
{
let credential: PhoneAuthCredential = PhoneAuthProvider.provider().credential(withVerificationID:strAuthVID, verificationCode: code)

}

enter image description here

关于ios - FireBase PhoneAuth 线程 1 : Fatal error: Unexpectedly found nil while unwrapping an Optional value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49746323/

相关文章:

ios - 在 opencv ios 中检测 C & CXX 编译器错误

swift - 使用相机向 UIPageViewController 添加多个图像

ios - 快速更新键盘预测栏建议

ios - 如果在新版本的 iOS 应用程序中整个代码发生更改(所有类、 Storyboard 更改、旧应用程序中没有数据库),会发生什么情况?它会工作还是崩溃?

ios - 根据 Swift 中的结构属性删除数组中的重复结构

ios - '? :' 表达式中的结果值具有不匹配的类型 '()' 和 'String?'

ios - 如何将对象作为 Objective C init 中的依赖项传递?

ios - strip 错误 : 'createTokenWithCard(_:completion:)' is unavailable

ios - 设备旋转通知未按预期运行 iOS Swift

ios - 在 Swift 中创建一个函数来播放特定频率的音调