【问题标题】:Thread 1: Found Nil while unwrapping / Swift [duplicate]线程 1:在展开 / Swift 时发现 Nil [重复]
【发布时间】:2018-11-27 11:09:34
【问题描述】:

我总是收到此错误消息:

线程 1:致命错误:在展开可选值时意外发现 nil

这是我的代码:

@IBAction func Push(_ sender: Any) {
    let vorname = Vornam.text!
    let userID = Auth.auth().currentUser!.uid

    ref.child("users").child(userID).child("Vorname").setValue(vorname)
}

你能帮帮我吗?我不明白我的问题:(

最好的祝福

【问题讨论】:

  • 根据你得到的错误信息:尽量不要强行打开一个可选项。

标签: swift multithreading firebase null forced-unwrapping


【解决方案1】:
let vorname = Vornam.text! <--- here 
let userID = Auth.auth().currentUser!.uid <--- or here 

试试这个,找出你的 nil 错误

@IBAction func Push(_ sender: Any) {
    if let vorname = Vornam.text {
        if let userID = Auth.auth().currentUser?.uid {
            ref.child("users").child(userID).child("Vorname").setValue(vorname)
        } else {
            print("Your FB user was not logged in & user was nil")
        }
    } else {
        print("Your Vornam.text was nil")
    } 
}

【讨论】:

    【解决方案2】:

    可能 Auth.auth().currentUser!.uid 返回一个 nil 值,尝试设置一些断点并查看编译器在哪里找到 nil 值!它也可以是具有 nil 值的 Vornam.text,在这种情况下,问题可能出在其他地方

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多