将Firebase数据转换为Swift数组

时间:2016-03-29 18:15:58

标签: swift firebase firebase-realtime-database

这就是我的Firebase数据的样子:

[ {
  "adress" : "Högåsstigen 10 332 33 Gislaved",
  "helg" : "18:00-02:00",
  "latitude" : 57.2985,
  "longitude" : 13.54326,
  "namn" : "Restaurang Åsen",
  "outlet" : 0,
  "phone" : "0371-123456",
  "star" : 0,
  "tag" : "Restaurang",
  "vardag" : "10:00-19:30"
}, {
  "adress" : "Högåsstigen 12 332 33 Gislaved",
  "helg" : "18:00-02:00",
  "latitude" : 57.9985,
  "longitude" : 13.94326,
  "namn" : "Kalles ställe",
  "outlet" : 0,
  "phone" : "0371-123456",
  "star" : 2,
  "tag" : "Restaurang",
  "vardag" : "10:00-19:30"
}, {
  "adress" : "Högåsstigen 15 332 33 Gislaved",
  "helg" : "18:00-02:00",
  "latitude" : 55.603384,
  "longitude" : 13.020619,
  "namn" : "Olles Pub",
  "outlet" : 0,
  "phone" : "0371-123456",
  "star" : 1,
  "tag" : "Krog",
  "vardag" : "10:00-19:30"
} ]

我想从数据库中获取“地址”。

我有问题将我从Firebase获取的数据转换为追加到数组中,以便稍后使用它们。我得到错误“类型'字符串'的值没有成员'生成器'”。

我不知道如何处理我的编码,任何建议?

var adresserArray = [String]()

let ref = Firebase(url: "https://HIDINGMYURL.firebaseio.com/")

ref.observeEventType(.ChildAdded, withBlock: {snapshot in
    let adresser = (snapshot.value.objectForKey("adress")) as! String!

    for add in adresser{
        adresserArray.append(adresser)
    }
})

1 个答案:

答案 0 :(得分:1)

你试图在这里迭代字符串

let adresser = (snapshot.value.objectForKey("adress")) as! String!

for add in adresser{
   adresserArray.append(adresser)
}

尝试用

替换此代码
let adresser = (snapshot.value.objectForKey("adress")) as! String
adresserArray.append(adresser)