Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.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
Swift UIButton标签显示Firebase的子值计数_Swift_Firebase_Firebase Realtime Database_Uibutton_Parent Child - Fatal编程技术网

Swift UIButton标签显示Firebase的子值计数

Swift UIButton标签显示Firebase的子值计数,swift,firebase,firebase-realtime-database,uibutton,parent-child,Swift,Firebase,Firebase Realtime Database,Uibutton,Parent Child,我正在尝试实现一个UIButton标题文本,以显示一个文本字符串,其中包含Firebase数据库中的子值数量。在viewDidLoad上,我希望按钮显示firebase数据库中存在的“place”子值的“#”。请参见下面的代码和firebase数据模型。提前谢谢 // JSON data model { "users" : { "CmtuebwVrmOG3n4uSsIK1b4FsSN2" : { "Places" : { "-KhDwZJvca9HedFl

我正在尝试实现一个UIButton标题文本,以显示一个文本字符串,其中包含Firebase数据库中的子值数量。在viewDidLoad上,我希望按钮显示firebase数据库中存在的“place”子值的“#”。请参见下面的代码和firebase数据模型。提前谢谢

// JSON data model
{
  "users" : {
    "CmtuebwVrmOG3n4uSsIK1b4FsSN2" : {
      "Places" : {
        "-KhDwZJvca9HedFluJ5O" : {
          "addedBy" : "CmtuebwVrmOG3n4uSsIK1b4FsSN2",
          "place" : "Edinburgh, UK",
          "timestamp" : 1.491678152020824E9
        },
        "-KhE7raDrM2RRs-N6FXg" : {
          "addedBy" : "CmtuebwVrmOG3n4uSsIK1b4FsSN2",
          "place" : "Hong Kong",
          "timestamp" : 1.49168137667081E9
        },
        "-KhFUaEjjhE3RBOw95fc" : {
          "addedBy" : "CmtuebwVrmOG3n4uSsIK1b4FsSN2",
          "place" : "Illinois, USA",
          "timestamp" : 1.491704112134812E9
        },
        "-Kk4EI1D7fuPyY2rvbdW" : {
          "addedBy" : "CmtuebwVrmOG3n4uSsIK1b4FsSN2",
          "place" : "Karnataka, India",
          "timestamp" : 1.494736515236516E9
        }
      },
    },  

    var placesTableView: PlacesTableVC?
    var placeList = [Place]()
    var placesDictionary = [String: Place]()
    var tableViewCount: Int?

    lazy var placesButton: UIButton = {
        let customButton = UIButton()
        customButton.backgroundColor = UIColor.blue
        // display the number of place values from firebase in the button title text
        customButton.setTitle("\(self.placesTableView?.placeList.count ?? 0) Visited Places", for: .normal)
        customButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
        customButton.setTitleColor(.white, for: .normal)
        customButton.addTarget(self, action: #selector(handleShowPlacesVC), for: .touchUpInside)

        return customButton
    }()

使用snapshot.childrenCount检索节点的子计数

let placesRef = self.ref.child("users/CmtuebwVrmOG3n4uSsIK1b4FsSN2/Places")
placesRef.observe(.value, with: { snapshot in
     print(snapshot.childrenCount)
     let count = String(snapshot.childrenCount)
     self.myButton.setTitle(count, for: .normal)
})

您的问题在哪里?您好-我想知道如何使UIButton标题文本显示Firebase数据库中存在的子值“place”的数量。例如,在firebase中提供的JSON中有4个“place”值,因此我希望UIButton标题文本显示为“4”。谢谢