Firebase Child必须是非空字符串且不包含

时间:2017-06-13 16:42:05

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

我正在使用Firebase在Swift中编写基本的消息传递应用程序。我完成了大部分应用程序,但在ChatViewController中我收到此错误消息:

*** Terminating app due to uncaught exception 'InvalidPathValidation', reason: '(child:) Must be a non-empty string and not contain '.' '#' '$' '[' or ']''
*** First throw call stack:

我的ChatViewController代码是:

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
    }

我曾尝试寻找具有相同问题的其他用户,但到目前为止我还没有成功解决。

1 个答案:

答案 0 :(得分:4)

可以从您收到的崩溃日志中提取答案。但首先,这里有一个小小的调试技巧:

添加例外断点

enter image description here

继续,您可以确定currentUserChatId的值无效。尝试对其进行硬编码(例如“someId”)并查看结果。确保它已定义并包含有效值。在ChattViewController课程中定义它。