swift – 在函数外部获取let值

前端之家收集整理的这篇文章主要介绍了swift – 在函数外部获取let值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
您好我是swift编程的新手,我想从loadData()获取标签值,以使用我的路径(引用)到我的数据库dbRef!.child(place“/ placeLabel”).我的意思是?!我读取“dio Con dio”节点上的数据.发生这种情况是因为价值所在的位置=“dio Con dio”.因此,我的应用程序不会从“Paradosiako – Panorama”节点加载数据.我想从数据库加载所有内容,我认为如果我可以创建位置值以更改到下一个节点,它可以读取所有数据.
import UIKit
    import FirebaseDatabase

    class PlacesTableViewController: UITableViewController {

        //MARK: Properties
        @IBOutlet weak var placesTableView: UITableView!

        //database reference
        var dbRef:FIRDatabaseReference?

        var places = [Places]()

        private var loadedLabels = [String: String]()
        private var loadedRatings = [String: Int]()

        //handler
        var handle:FIRDatabaseHandle?

        override func viewDidLoad() {
            super.viewDidLoad()

            dbRef = FIRDatabase.database().reference()

            // Loads data to cell.
            loadData()
        }

                private func loadData() {

                let place = "dio Con dio"

                dbRef!.child(place+"/placeLabel").observe(.childAdded,with: {
                    (snapshot) in
                    let label = snapshot.value as! String
                    self.updatePlace(snapshot.key,label: label)
                })
                dbRef!.child(place+"/rating").observe(.childAdded,with: {
                    (snapshot) in
                    let rating = snapshot.value as! Int
                    self.updatePlace(snapshot.key,rating: rating)
                })


            }

            private func updatePlace(_ key: String,label: String? = nil,rating: Int? = nil) {
                if let label = label {
                    loadedLabels[key] = label
                }
                if let rating = rating {
                    loadedRatings[key] = rating
                }
                guard let label = loadedLabels[key],let rating = loadedRatings[key] else {
                    return
                }
                if let place = Places(name: label,rating: rating) {
                    places.append(place)
                    placesTableView.reloadData()
                }
            }
        }

(这个问题是跟进from this one.)

对于新的数据库架构,请尝试以下方法
private func loadData() {
    dbRef!.observe(.childAdded) {
    (placeSnapshot) in
        print("Adding place \(placeSnapshot.key)...")

        // Load each label on the "placeLabel" list. 
        let labels = placeSnapshot.childSnapshot(forPath: "placeLabel")
        for (key,label) in labels.value as! [String: String] {
            self.updatePlace(key,label: label)
        }

        // Load each rating in the "rating" list. 
        let ratings = placeSnapshot.childSnapshot(forPath: "rating")
        for (key,rating) in ratings.value as! [String: Int] {
            self.updatePlace(key,rating: rating)
        }
    }
}

updatePlace函数只是对给定位置的标签和评级属性进行分组,这些属性由loadData单独加载.

地方删除.当然,上面的代码不会处理删除位置.为此,您还需要监听childRemoved事件.

原文链接:https://www.f2er.com/swift/319955.html

猜你在找的Swift相关文章