从Firebase iOS Swift中检索多个阵列

时间:2017-03-22 03:33:59

标签: ios swift firebase firebase-realtime-database

我的Firebase数据库中有多个对象数组。 我想检索每个数组中的项目并将它们放入Swift词典中。如何获取值并将它们从Firebase的快照中插入字典?

我的Firebase数据库的组织方式:

data: {
  firstKey: [
    {object},
    {object},
    ...
  ],
  secondKey: [
    {object},
    {object},
    ...
  ],
  ...
}

从Firebase检索快照的行:

ref.child("data").observe(.value, with: { (snapshot) in })

1 个答案:

答案 0 :(得分:1)

首先为您的快照创建一个模型;

<xp:this.data>
    <xp:dominoDocument var="document1" formName="Topic"
        computeWithForm="onsave" />
</xp:this.data>

<xp:inputText id="inputTopic" value="#{document1.topic}">
    <xp:this.converter>
        <xp:customConverter getAsString="#{javascript:value}"
            getAsObject="#{javascript:value.ltrim().rtrim()}" />
    </xp:this.converter>
</xp:inputText>

<xp:br />
<xp:br />

<xp:button id="buttonSave" value="Save">
    <xp:eventHandler event="onclick" submit="true"
        refreshMode="complete" action="#{javascript:document1.save()}" />
</xp:button>

<xp:br />
<xp:br />

<xp:text id="computedField1" escape="true" value="#{document1.topic}" />

并在回调中获取每个数据;

class YourModel: NSObject {
         var field1: String!
         var field2: Int!
         var field3: Bool!

         init(snapshot: FIRDataSnapshot) {
            if let snapshotDict = snapshot.value as? Dictionary<String, Any> {
                field1 = snapshotDict["field1"] as? String
                field2 = snapshotDict["field2"] as? Int
                field3 = snapshotDict["field3"] as? Bool

            }
        }
       }