NSDictionary的值为none

时间:2017-05-09 06:22:08

标签: ios firebase swift3 firebase-realtime-database nsdictionary

我希望用户添加好友。但是,每当我点击 addFriend 按钮时,我都会得到一个thread 1的断点。没有其他错误信息而不是11db。 我现在发现“otherUser”的NSDictionary的值为none。这就是一切都不起作用的原因。但是我不知道为什么会这样。在同一个NSDictionary中,我获得了用户名和个人资料Pic。那为什么没有?

var otherUser: NSDictionary?

override func viewDidLoad() {
    super.viewDidLoad()
databaseRef.child("user").child(self.otherUser?["uid"] as! String).observe(.value, with: { (snapshot) in

        let uid = self.otherUser?["uid"] as! String
        self.otherUser = snapshot.value as? NSDictionary
        self.otherUser?.setValue(uid, forKey: "uid")

    }) { (error) in
        print(error.localizedDescription)
    }
}
@IBAction func addFriend(_ sender: Any) {

    if self.befreunden.titleLabel?.text == "Als Freund hinzufügen" {

    let friendRef = "befreundet/\(self.loggedinUSerData?["uid"] as! String)/\(self.otherUser?["uid"] as! String)"

    let BefreundenData = ["username":self.otherUser?["username"] as? String,
                              "ProfilePic":self.otherUser?["urltoImage"] as! String!]


    let childUpdats = [friendRef:BefreundenData]

        databaseRef.updateChildValues(childUpdats)

        }else {

            FreundeAnzahl = self.otherUser?["AnzahlFreunde"] as! Int + 1

        }
}

修改:

我刚刚发现,otherUser的NSDictionary拥有viewdidLoad中所需的所有值。但是一旦我在addFriend函数中调用了otherUser的uid,它就会返回一个nil的值。 为什么会这样?

1 个答案:

答案 0 :(得分:2)

我通过简单地将otheruser NSDictionary的uid提供给我在viewdidLoad中创建的新变量来修复该问题。 这样我以后就不会遇到任何关于解包选项的问题。