1

I want to make a user query, removing the name of each, and go keeping the names in an array, then display them on a table. The problem is I can not keep them in the settlement, how can I fix it? Thus I make the query to extract the names of users:

var users = [String]()
let ref = Firebase(url:"https:mydatabase/users")
ref.queryOrderedByChild("name").observeEventType(.ChildAdded,
    withBlock: { snapshot in
        if let username = snapshot.value["name"] as! String {
            self.users.append(username)
            print("username")
        }
    })

So I have my users table in firebase

firebase screenshot

The username var does have the name, but when adding the content of the var the settlement, does not, at the time of execution of the application does not throw any errors.

4
  • 1
    Sorry could you please elaborate? What do you mean by settlement? Commented Apr 5, 2016 at 21:04
  • sorry, my english is very bad, i can't save the "username" var, in the "users" array Commented Apr 6, 2016 at 0:28
  • what happens when you print(username) (don't use quotes)? Commented Apr 6, 2016 at 13:03
  • ok, the problem is when I want to save the variable (usersname) in the array(users), it does not., help mee Commented Apr 6, 2016 at 16:24

1 Answer 1

0

There are just a few typo's your code

Try this:

let usersRef = self.myRootRef.childByAppendingPath("users")
usersRef.queryOrderedByChild("name").observeEventType(.ChildAdded, withBlock: { snapshot in
        if let username = snapshot.value["name"] as? String {
            self.userArray.append(username)
            print("\(username)")
        }
    })

Be sure to define the userArray as a property of the class so it will be available to other functions.

class MyClass: NSObject {

    let myRootRef = Firebase(url:"https://your-app.firebaseio.com")
    var userArray = [String]()
Sign up to request clarification or add additional context in comments.

1 Comment

i'm sorry, thanks for answared, you helped me a lot of

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.