Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase数据库中的Swift存储数据未出现在tableview中_Swift_Firebase_Firebase Realtime Database - Fatal编程技术网

Firebase数据库中的Swift存储数据未出现在tableview中

Firebase数据库中的Swift存储数据未出现在tableview中,swift,firebase,firebase-realtime-database,Swift,Firebase,Firebase Realtime Database,我正在Swift中创建一个餐馆评级应用程序,供用户对某些餐馆发表评论。该应用程序不需要用户注册或登录,因此我认为我不需要用户ID。我可以成功地将用户的评论引导到数据库中的特定餐厅,因此每个餐厅都有自己的评论部分(存储为locationName)。添加评论没有问题。尽管如此,我似乎无法将firebase数据库中的评论显示到应用程序中。这是我的代码,如果有任何帮助,我将不胜感激,非常感谢 var comments = [String]() var locationName = String() //

我正在Swift中创建一个餐馆评级应用程序,供用户对某些餐馆发表评论。该应用程序不需要用户注册或登录,因此我认为我不需要用户ID。我可以成功地将用户的评论引导到数据库中的特定餐厅,因此每个餐厅都有自己的评论部分(存储为locationName)。添加评论没有问题。尽管如此,我似乎无法将firebase数据库中的评论显示到应用程序中。这是我的代码,如果有任何帮助,我将不胜感激,非常感谢

var comments = [String]()
var locationName = String() // created a specific node for each restaurant to display comments in the database
var ref: DatabaseReference?
var databaseHandle: DatabaseHandle?


func loadComments() { //in viewDidLoad

    ref = Database.database().reference()

    databaseHandle = ref?.child("comments").child(locationName).observe(.childAdded, with: { (snapshot) in

        let comment = snapshot.value as? String

        if let actualComment = comment {

            self.comments.append(actualComment)

            self.tableView.reloadData()

        }
    })


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return comments.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "CellFour")
    cell.textLabel!.text = comments[indexPath.row]
    return cell
}

Firebase数据库上有安全规则吗?u未进行身份验证的事实可能会导致问题。转到Firebase控制台-数据库-安全规则,检查未经身份验证的用户是否可以访问您的数据库。@GalShahar在规则中,read和write都设置为true。我不希望用户创建一个帐户进行评论,因此我不确定如何进一步提高安全性。甚至没有一个人知道我做错了什么-(尝试打印从数据库中提取的注释。在“let comment=snapshot.value as?String”之后添加print(“comment”,comment)。您看到了什么吗?或者它没有打印任何内容。我认为您的cellForRowAt有问题function@GalShahar就在“let comment=snapshot.value as?String”之后,我插入了print(“comment”,comment)我得到了零评论我如何解决这个问题?