简体   繁体   English

如何从查询中保存数据,在字符串数组中,Firebase iOS?

[英]How to save data from query, in array of string, Firebase iOS?

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中有我的用户表

firebase截图

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. 用户名var确实具有名称,但是当添加var的内容时,结果不会,在执行应用程序时不会抛出任何错误。

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. 请务必将userArray定义为类的属性,以便其他函数可以使用它。

class MyClass: NSObject {

    let myRootRef = Firebase(url:"https://your-app.firebaseio.com")
    var userArray = [String]()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM