在 Swift 中访问函数外部的变量 [英] Accessing variable outside function in Swift

查看:22
本文介绍了在 Swift 中访问函数外部的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问此函数外的帖子",以便我可以在该函数外调用 tableview.reloaddata().每次有一个键进入 qeoquery 时,下面的代码都会调用 tableview.reloaddata().我只想重新加载一次.但是当我在 viewDidLoad 中尝试时,posts"数组是空的.怎么办?

I want to access "posts" outside this function so that I can call tableview.reloaddata() outside the function. The code under calls tableview.reloaddata() everytime a key has entered the qeoquery. I want to only reload it one time. But when I try in viewDidLoad, the "posts" array is empty. What to do?

在函数外声明的帖子:

var  posts = [Post]()

功能:

func fetchData(){


        geofireRef = Database.database().reference().child("LOCATION")
        geofire = GeoFire(firebaseRef: geofireRef)
        let geoQuery = geofire?.query(at: myLoc, withRadius: 5.0)

        geoQuery?.observe(.keyEntered, with: { (key: String!, location: CLLocation!) in
            print("KEYKEY: \(key)")
            let dbRef = Database.database().reference().child("posts")
            let query = dbRef.queryOrdered(byChild: "\(key)")
            query.observeSingleEvent(of: .value, with: { (snapshot) in
                if let snapshot = snapshot.children.allObjects as? [DataSnapshot]{
                    for snap in snapshot {
                        if let postDict = snap.value as? Dictionary<String, Any>{
                            let key = snap.key
                            let post = Post.init(postKey: key, postData: postDict)


                            self.posts.append(post)
                        }
                    }
                }
                //self.posts.reverse()
                //self.tableView.reloadData()
                //print("POSTS: \(self.posts)")
            })
        })
    }

推荐答案

你可以在设置你的帖子变量后立即调用 tableView.reloadData() ,或者你可以在您的帖子变量上放置一个 didSet 观察者,并在设置后重新加载您的 tableView.如果您使用第二个选项,您将需要重组解析器以仅设置一次帖子,而不是一次附加一个项目.看起来像这样:

You can either call tableView.reloadData() right after setting your posts variable (like you have commented out), or you can put a didSet observer on your posts variable and reload your tableView after it's set. If you go with that second option you'll want to restructure your parser to set the posts only once, rather than appending items one at a time. That would look something like this:

var posts = [Post]() {
    didSet {
        self.tableView.reloadData()
    }
}

geoQuery?.observe(.keyEntered, with: { (key: String!, location: CLLocation!) in
        print("KEYKEY: \(key)")
        let dbRef = Database.database().reference().child("posts")
        let query = dbRef.queryOrdered(byChild: "\(key)")
        query.observeSingleEvent(of: .value, with: { (snapshot) in
            if let snapshot = snapshot.children.allObjects as? [DataSnapshot]{
                //Create a variable to hold posts until you've parsed all of them
                var foundPosts = [Post]()
                for snap in snapshot {
                    if let postDict = snap.value as? Dictionary<String, Any>{
                        let key = snap.key
                        let post = Post.init(postKey: key, postData: postDict)
                        foundPosts.append(post)
                    }
                }
                //Set the posts to be all the found posts
                self.posts = foundPosts
            }
        })
    })

这篇关于在 Swift 中访问函数外部的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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