Firebase Child必须是非空字符串,并且不能包含 [英] Firebase Child must be a non-empty string and not contain
问题描述
我正在使用Firebase在Swift中编写一个基本的消息传递应用程序.我已经完成了大部分应用程序的工作,但是在ChatViewController中,我收到了以下错误消息:
I am coding a basic messaging app in Swift with Firebase. I am done with the majority of the app but in the ChatViewController Im getting this error message:
*** Terminating app due to uncaught exception 'InvalidPathValidation', reason: '(child:) Must be a non-empty string and not contain '.' '#' '$' '[' or ']''
*** First throw call stack:
我的ChatViewController代码是:
My ChatViewController code is:
import UIKit
import Firebase
struct Post {
let messageTextt: String!
}
class ChattViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let database = DatabaseReference!.self
@IBOutlet weak var messageText: UITextField!
@IBOutlet weak var tableViewC: UITableView!
var posts = [Post]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tableViewC.delegate = self
tableViewC.dataSource = self
let database = Database.database().reference()
database.child("Posts").child(currentUserChatId).queryOrderedByKey().observe(.childAdded, with: {
snapshot in
let messageTextt = (snapshot.value as? NSDictionary)?["messageTextt"] as? String ?? ""
self.posts.insert(Post(messageTextt : messageTextt), at: 0)
self.tableViewC.reloadData()
})
}
@IBAction func sendMessageText(_ sender: Any) {
if messageText.text != "" {
let uid = Auth.auth().currentUser?.uid
let database = Database.database().reference()
let bodyData : [String : Any] = ["uid" : uid!, "messageTextt" : messageText.text!]
database.child("posts").child(currentUserChatId).childByAutoId().setValue(bodyData)
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt IndexPath:
IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: IndexPath)
let messageTextt = cell.viewWithTag(1) as! UITextView
messageTextt.text = posts[IndexPath.row].messageTextt
return cell
}
我试图寻找其他具有相同问题的用户,但到目前为止,我还没有解决.
I have tried looking for other users with the same problem but I haven't managed to solve it so far.
推荐答案
可以从您收到的崩溃日志中提取答案.但是首先,这里有一些调试提示:
The answer can be extracted from the crash log you're receiving. But first, here's a little tip for debugging:
添加例外断点
继续,可以确定您的currentUserChatId
的值无效.尝试对其进行硬编码(例如"someId"),然后查看结果.确保已定义并包含有效值.在您的ChattViewController
类中定义它.
Going on, it is certain that your currentUserChatId
has an invalid value. Try hard-coding it (ex. "someId") and see the result. Make sure it is defined and contains a valid value. Define it in your ChattViewController
class.
这篇关于Firebase Child必须是非空字符串,并且不能包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!