Swift Firebase读了一个孩子的孩子 [英] Swift Firebase read children of a child
问题描述
因此,我试图读取autoID子项的子项,下面是我的Firebase数据库的图片,下面是应该检索描述"和名称"的值并插入的方法它们变成变量.
So I am trying to read the children of the autoID children beneath "Recipes" below is a picture of my Firebase database and beneath that is the method that is supposed to retrieve the value of "Description" and "Name" and insert them into variables.
运行该应用程序时出现的当前错误是:
The error that I am currently getting when running the app is this:
Could not cast value of type '__NSCFString' (0x10ad2afb8) to 'NSDictionary' (0x10ad2bfa8).
ref = Database.database().reference()
databaseHandle = ref?.child("Recipes").observe(.childAdded) { (snapshot) in
for snap in snapshot.children
{
let recipeSnap = snap as! DataSnapshot
let recipeID = recipeSnap.key
let dict = recipeSnap.value as! [String:AnyObject]
let recipeName = dict["Name"] as! String
let recipeDescription = dict["Description"] as! String
print("key = \(recipeID) and name = \(recipeName) and description = \(recipeDescription)")
}
}
打印语句就在这里进行测试.
The print statement is just there for testing.
推荐答案
尝试以下操作,让我知道它现在是否有效:
Try the following and let me know if it works now:
// SEARCHES FOR SHARING CODE IN DATABASE (ONLINE)
let parentRef = Database.database().reference().child("Recipes")
parentRef.observeSingleEvent(of: .value, with: { snapshot in
// SHOWING WHATEVER WAS RECEIVED FROM THE SERVER JUST AS A CONFIRMATION. FEEL FREE TO DELETE THIS LINE.
print(snapshot)
// PROCESSES VALUES RECEIVED FROM SERVER
if ( snapshot.value is NSNull ) {
// DATA WAS NOT FOUND
print("– – – Data was not found – – –")
} else {
// DATA WAS FOUND
for user_child in (snapshot.children) {
let user_snap = user_child as! DataSnapshot
let dict = user_snap.value as! [String: String?]
// DEFINE VARIABLES FOR LABELS
let recipeName = dict["Name"] as? String
let recipeDescription = dict["Description"] as? String
print("– – – Data for the recipe \(recipeName) with the description \(recipeDescription) was found successfully! – – –")
}
}
}
如果您只想检索一种特定于 食谱的名称和说明,则应将第三行更改为
If you only want to retrieve the name and description for one specific recipe, you should change the third line to
parentRef.queryEqual(toValue:DefineWhatToSearchForHere).observeSingleEvent(of: .value, with: { snapshot in
如果您一直想进行更新以反映更改,则可以使用计时器每x秒调用一次此函数,然后将其添加到 override func viewDidLoad()
例如
If you constantly want to update to reflect changes, you can either call this function every x seconds using a timer and adding it to override func viewDidLoad()
such as
time = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(ViewController.updateFBData), userInfo: nil, repeats: true)
在创建一个名为 func updateFBData()
的函数后,您可以执行您想做的任何操作来获取新数据(参见上文),并在定义的 timeInterval
中调用它
after creating a function called func updateFBData()
in which you do whatever you want to do to get new data (see above) and calling it in a defined timeInterval
或者您可以在此出色的教程中做AttilaHegedüs./a>
or you can do what Attila Hegedüs in this excellent tutorial.
这篇关于Swift Firebase读了一个孩子的孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!