Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.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
Ios 如何在swift 4中读取childByAutoId()的值?_Ios_Swift_Uitableview_Firebase Realtime Database - Fatal编程技术网

Ios 如何在swift 4中读取childByAutoId()的值?

Ios 如何在swift 4中读取childByAutoId()的值?,ios,swift,uitableview,firebase-realtime-database,Ios,Swift,Uitableview,Firebase Realtime Database,我是iOS开发新手,我使用childByAutoId将一大块数据存储到firebase数据库,下面存储用户名、photoURL、textMessage和用户UID。我正在tableView单元格中检索此数据。现在我想要的是,当我点击其中一个POST或tableView单元格时,我想要打印childByAutoId函数生成的唯一ID。如何做到这一点 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPat

我是iOS开发新手,我使用childByAutoId将一大块数据存储到firebase数据库,下面存储用户名、photoURL、textMessage和用户UID。我正在tableView单元格中检索此数据。现在我想要的是,当我点击其中一个POST或tableView单元格时,我想要打印childByAutoId函数生成的唯一ID。如何做到这一点

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let name = Auth.auth().currentUser?.displayName

    let indexPath = tableView.indexPathForSelectedRow

    let alertController = UIAlertController(title: "Trial", message: "You Selected "+name!, preferredStyle: .alert)

    let defaultAction = UIAlertAction(title: "Close Alert", style: .default, handler: nil)
    alertController.addAction(defaultAction)

    present(alertController, animated: true, completion: nil)

}
我用这个代码来捕捉细胞上的信号。那么,在点击特定单元格后,有助于获取其子ID的代码是什么? 我想把它打印在日志里


这就是我的列表的样子。

每个单元格都有一个indexPath,其中包含关于其行和节的信息:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    //Side note: the name of a user shouldn't be optional 
    let name = Auth.auth().currentUser?.displayName ?? ""
    let message = "You Selected " + name + " in cell number : " + \(indexPath.row) 

    let alertController = UIAlertController(title: "Trial", message: message, preferredStyle: .alert)

    let defaultAction = UIAlertAction(title: "Close Alert", style: .default, handler: nil)
    alertController.addAction(defaultAction)

    present(alertController, animated: true, completion: nil)
}

如果要访问任何特定数据的密钥,必须进行firebase查询。我相信你有一些独特的价值来识别你的数据

为了获得唯一密钥,可以使用以下函数:

func getUniqueFirebaseKey(){
    let ref = Database.database().reference()
    ref.child("CHILD_NAME")
    .queryEqual(toValue: "UNIQUE_ID")
    .observe(.value, with: { (snapshot: DataSnapshot) in`

        if let snap = snapShot.value as? [String:Any] {

            for key in snap.keys{
                return key
            }
    })
}
通过此操作,您可以获得firebase的自动ID密钥


但更好的选择是创建一个模型并在解析firebase数据时存储该密钥。

谢谢回复!,使用这段代码,我得到了每一行的索引,比如0,1,2,3,但是我想访问分配给它的键@carpsen90,那么你想打印firebase DB中的子id值吗?或者单元格的唯一id?firebase数据库@RahimKhalidNow中的子id假设我点击第0个单元格,那么根据此信息,我如何获得分配给它的子id@Rahim Khalid第0单元你有一些对象,比如名字、消息、时间和其他东西,对吗?通过上面的代码,我可以检索所有子id,但我想检索帖子的特定子id,一个被点击的子id@Rahim KhalidOk,因此您将Post的所有对象都放在一个数组中,用于填充表。因此,您可以通过postArray[indexPath.row].autoIdposts[indexPath.row].autoId来访问数据,并将autoId替换为用于分析autoId的属性