textfield in closure swift

Refresh

2 weeks ago

Views

13 time

0
    let creatorDB = Database.database().reference().child("A")
    creatorDB.queryLimited(toLast: 1).observe(.childAdded) { (snapshot) in

        let B = snapshot.value as! String
        print(B)

        let messageDB = Database.database().reference().child("\(B))/Messages/")


        let messageDictionary = ["Sender": Auth.auth().currentUser?.uid , "MessageBody": TextField.text!]

            messageDB.childByAutoId().setValue(messageDictionary) {

                (error, reference) in
                if error != nil {
                    print(error!)
                } else {
                    print("message saved succesfully")

                    self.chatTextField.isEnabled = true
                    self.sendButton.isEnabled = true
                    self.TextField.text = ""
                }

        }

    }

when I use this code, I can't retrieve the Messagebody, I guess self.textfield.text doesn't work, how can I fix for the self.textfield.text in closure

0 answers