0

This is how my Firebase data looks like:

[ {
  "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"
} ]

I want to grab "adress" from the database.

I'm having problem to convert the data I grab from Firebase to append into an array so I can use them later. I get the error "Value of type 'String' has no member 'generator'".

I have no idea how to proceed in my coding, any suggestions?

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)
    }
})
2
  • You've included a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export button in your Firebase database. Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. Commented Mar 29, 2016 at 18:31
  • Done, however Peter K helped me but I get some wierd data when I print my array after using his advice, example below. Maybe you have an idea. Thanks Frank. Commented Mar 29, 2016 at 19:19

1 Answer 1

1

You are trying to iterate over String here

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

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

try just replace this code with

let adresser = (snapshot.value.objectForKey("adress")) as! String
adresserArray.append(adresser)
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you so much, however I get some wierd converting... It's like 3 or 4 arrays or [] in there, any idea why? ["Högåsstigen 10 332 33 Gislaved"] ["Högåsstigen 10 332 33 Gislaved", "Högåsstigen 12 332 33 Gislaved"] ["Högåsstigen 10 332 33 Gislaved", "Högåsstigen 12 332 33 Gislaved", "Högåsstigen 15 332 33 Gislaved"]
Could you show more code where dos you get this result?
I used - print(self.adresserArray) directly under the code you wanted me to replace. Then I get the last thing I commented in my console...
Seems like I get it. Your code inside ref.observeEventType callback block runs on every child added, so if you add print inside, you will print this array after each dress insert. Your log is just 3 sprints of array 1 st time with one dress second with 2 etc.
What should I do to make it look normal so I can actually use the addresses later on?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.