Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

swift - Getting a let value outside a function

Hello I'm new at swift programming and i want to get label value from loadData() to use use for my path (reference) to my database on dbRef!.child(place+"/placeLabel"). What I mean?! I read data that are on "Dio Con Dio" node. That happens because the value place is let place = "Dio Con Dio". So, my application doesn't load data from "Paradosiako - Panorama" node. I want to load everything from database and i thought that if i could make place value to change to the next node, it could read all the data.

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

**My Database**

(This question is a follow up from this one.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...