【发布时间】:2017-11-15 13:39:54
【问题描述】:
我正在使用 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
}
我已经尝试寻找其他有相同问题的用户,但到目前为止我还没有设法解决。
【问题讨论】:
-
您能否更新您的问题以在代码中包含错误发生的位置?这些信息将帮助我给你一个更好的答案。没有它,我将不得不假设它在调用
.child()时发生。看起来您正在将currentUserChatId传递给.child()。你可以为currentUserChatId添加一个打印语句,以便我看到它的价值吗?谢谢! -
我不确定错误实际发生在哪里。
-
currentUserChatId是什么值? -
link 那是创建和使用 currentUserChatId 的 tableviewcontroller。
标签: ios swift firebase swift3 firebase-realtime-database