如何设置用户个人资料

我声明了一个常量以使我的userID可读,但是Xcode强迫我解包userID以使我的代码有效如何解决此错误

  

使用'!'强制展开。如果可选值,则中止执行   包含“ nil”

DatabaseRef = Database.database().reference()
let userID = Auth.auth().currentUser?.uid
DatabaseRef.child("users").child(userID!).observeSingleEvent(of: .value,with: { (snapshot) in
bin_swnswn 回答:如何设置用户个人资料

这是因为'currentUser'是可选的:

let userID = Auth.auth().currentUser?.uid

要么强制解开它,要么使用'??'提供数据类型的默认值。

您也可以尝试使用'if let'或'guard'语句。

仔细阅读以下文档:

  

https://docs.swift.org/swift-book/LanguageGuide/OptionalChaining.html

本文链接:https://www.f2er.com/2967101.html

大家都在问