从Firebase读取/检索数据 [英] Reading/Retrieving Data From Firebase
问题描述
{
数据库= {
开尔文= {
Institution = xxx;
经验年数=2.5;
位置=伦敦;
};
Sophia = {
Institution = xxxx;
经验年数= 3;
位置=伦敦;
};
};
用户= {
SOOlIFsjn3839jcmlBbVEnSRH3 = {
email =testing@gmail.com;
密码= 1234567;
};
};
$ b现在我想要分开数据以便显示这样的内容:
var ref:FIRDatabaseReference!
ref = FIRDatabase.database()。reference()
ref.observe(FIRDataEventType.value,其中:{(snapshot)in
如果让mydata = snapshot.value as NSDictionary {
print(mydata)
如果让namedata = mydata [Database] as?NSArray {
print(namedata [0])//打印开尔文
print(namedata [1])//打印索非亚
}
然而,Swift不允许我这样做,它告诉我我不能将一种类型的NSDictionary转换为NSArray。任何想法?非常感谢。
Firebase集合是键值对,对于JSON的每个键,都可以找到相应的值。
所以你需要转换为String,Object的 NSDictionary
,该字典中的键将是 Kelvin
和 Sophia
。每个值都是相应键下的值。
I am a beginner to Swift 3 (using iOS 8.3). I have been trying to figure out a way to separate the data but got no luck. Basically, I am able to extract the following data from Firebase:
{
Database = {
Kelvin = {
Institution = xxx;
"Years of Experience" = "2.5";
location = London;
};
Sophia = {
Institution = xxxx;
"Years of Experience" = 3;
location = London;
};
};
Users = {
SOOlIFsjn3839jcmlBbVEnSRH3 = {
email = "testing@gmail.com";
password = 1234567;
};
};
}
But now I want to separate the data so that it will show something like this:
var ref: FIRDatabaseReference!
ref = FIRDatabase.database().reference()
ref.observe(FIRDataEventType.value, with: { (snapshot) in
if let mydata = snapshot.value as? NSDictionary {
print(mydata)
if let namedata = mydata["Database"] as? NSArray {
print(namedata[0]) //Prints Kelvin
print(namedata[1]) //Prints Sofia
}
However, Swift doesn't allow me to do so, it tells me I cannot convert a type of NSDictionary to NSArray. Any ideas? Very much appreciated.
Firebase collections are key-value pair. For each key of the JSON, you can find the corresponding value.
So you'll need to convert to an NSDictionary
of String, Object. The keys in that dictionary will be Kelvin
and Sophia
. The value for each is the value under the corresponding key.
这篇关于从Firebase读取/检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!