2017-05-26 93 views
0

enter image description here使用迅速3如何从火力

retrive另一个数据内的数据我试图在一个特定的数据数据取诸如在“最小”内部AGE_RANGE这等于21 虽然“总”'内总结'即朋友内部。 我只有成功获取正常的数据,如姓名和性别.... 你的帮助将是非常可观的

+0

那么,什么是你的问题?如何从朋友/总结/总数中获取数据?或者是什么? –

+0

是啊即时通讯试图获取数据中的数据并将其保存为dictonary或许 –

+0

请发布Firebase结构作为文本请不要图像。图片不可搜索,如果我们需要在答案中使用结构,我们必须重新输入。您可以从Firebase控制台获取Firebase结构 - >右键点三个点 - >导出JSON。 – Jay

回答

1

斯威夫特3 &的Xcode 8.3.2

我希望这个代码将解决您的问题

// Create constant of baseURL (Make your life easier) 
    let baseURl = Database.database().reference() 

    // create reference that u want to fetch 
    let refToFetch = baseURl.child("users").child("uid").child("FB result").child("age_range") 

    // fetch the value 
    refToFetch.observeSingleEvent(of: .value, with: { (snapshot) in 
     // do something with value for key "min" 
     print(snapshot.value(forKey: "min") ?? "no value") 
     if let value: Int = snapshot.value(forKey: "min") as? Int { 
      // You got the value 
      print("value of min is: \(value)") 
     } 
    }) { (error) in 
     print(error, "Failed to fetch value") 
    } 
+0

错误是什么?给我看你的控制台,所以我可以为你解决这个问题 –

+0

@OT AMD我编辑了我的答案,所以它会更清楚你的...请投票,如果我的答案是为你工作,谢谢你 –

1

如果我正确理解你的问题,你想要从儿童的孩子检索数据。

ref?.child("users").child(uid).child("youCanAddAsManyOfTheseAsYouWishToAccessChildrenOfChildren").observeSingleEvent(of: .value, with: { (snapshot) in 
     //you can either access children in the line above or by: 
     snapshot.childSnapshot(forPath: "childName").value 
}) 

如果这不是您的问题的答案,请在下面评论。

编辑:这里是一个确切的例子给你。

ref?.child("users").child(uid).child("FB result").child("age_range").child("min").observeSingleEvent(of: .value, with: { (snapshot) in 
     //you can either access children in the line above or by: 
     print(snapshot.value) 
}) 
+0

差不多我的答案,但我不能成功到目前为止...请帮助我得到一个实例如何取代这个:“你cancanasasanyanyfasseasyouwishToAccessChildrenOfChildren”如果我想获得21的价值! –

+0

@OTAMD我编辑了我的回答 – joshLor

+0

@OTAMD您还可以使用'ref.child(“users/uid/FB result/age_range/min”)''通过路径访问孩子。您可能会发现[this](https://stackoverflow.com/a/40567781/4962554)有助于理解Firebase的结构。 – eshirima