NSDictionary is none

I want the user to add friends. However, whenever I click on the addFriend button , I get a breakpoint with thread 1

. No more information on errors, not 11db

. I already figured out that NSDictionary

"otherUser" is set to none. For this reason, everything does not work. However, I don't know why this is the case. From the same NSDictionary

, I get the username and the Pic profile. Why is it then?

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

        }
}

      

Edit:

I just found out that another user's NSDictionary has all the values ​​needed inside the viewdidLoad. But as soon as I call another user's uid in the addFriend function, it returns nil. Why is this happening?

+3


source to share


1 answer


I fixed the problem simply by providing the uid of another NSDictionary user with a new variable that I created in viewdidLoad. This way I don't run into any problems regarding the deployment options later.



+2


source







All Articles