如何保存数据从查询,字符串数组,Firebase iOS? [英] How to save data from query, in array of string, Firebase iOS?

查看:222
本文介绍了如何保存数据从查询,字符串数组,Firebase iOS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想进行用户查询,删除每个的名称,并保持在数组中的名称,然后将其显示在表上。问题是我不能让他们在解决,我怎么能解决它?因此我进行查询以提取用户的名称:

  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)
}
})



所以我在firebase中有我的用户表





用户名var确实有名称,但是当添加var的内容时,不会在执行应用程序时

解决方案

只有一些错字您的代码



尝试:

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

请务必将userArray定义为类的属性,以便其他函数可用。

  class MyClass:NSObject {

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


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

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.

解决方案

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]()

这篇关于如何保存数据从查询,字符串数组,Firebase iOS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆