我的上传代码在这里
import UIKit
import Firebase
class ChatViewController: UIViewController {
let chatRef = FIRDatabase.database().reference().child("chat")
let userUid = FIRAuth.auth()?.currentUser?.uid
var userName = ""
@IBOutlet weak var topBar: UINavigationItem!
@IBOutlet weak var containerView: UIView!
@IBOutlet var inputTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
topBar.title = "Chat Log Controller"
FIRDatabase.database().reference().child("users/\(userUid!)/name").observe(.value) { (snap: FIRDataSnapshot) in
self.userName = (snap.value! as! String).description
}
}
@IBAction func handleSend(_ sender: AnyObject) {
let childChatRef = chatRef.childByAutoId()
let message = inputTextField.text!
childChatRef.child("text").setValue(message)
print(inputTextField.text)
}
@IBAction func handleSendByEnter(_ sender: AnyObject) {
let childChatRef = chatRef.childByAutoId()
let message = inputTextField.text!
print(userName)
childChatRef.child("name").setValue(userName)
childChatRef.child("text").setValue(message)
print(inputTextField.text)
}
}
文字已成功上传但是 它不会打印userName,也不会将其上载到Firebase数据库 但用户名是坚果零!
答案 0 :(得分:2)
尝试使用您的观察者代码,
ref.observeSingleEventOfType(.Value, withBlock: { snapshot in
}
答案 1 :(得分:0)
只需self.username = snap.value! as! String
它会解决你的问题。