从Firebase读取/检索数据 [英] Reading/Retrieving Data From Firebase

查看:145
本文介绍了从Firebase读取/检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Swift 3的初学者(使用iOS 8.3)。我一直在想办法分离数据,但没有运气。基本上,我可以从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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆