在函数之外获得一个值 [英] Getting a let value outside a function
问题描述
你好,我是swift编程的新手,我想从 loadData()
使用标签
值来使用使用我的路径(引用)到我的数据库上 dbRef!.child(place +/ placeLabel)
。我的意思是?!我阅读Dio Con Dio节点上的数据。发生这种情况是因为值 place
是 let place =Dio Con Dio
。所以,我的应用程序不会从Paradosiako - Panorama节点加载数据。我想加载从数据库中的一切,我认为,如果我可以使地点
值更改为下一个节点,它可以读取所有的数据。
导入UIKit
导入FirebaseDatabase
类PlacesTableViewController:UITableViewController {
// MARK:Properties
@IBOutlet weak var placesTableView:UITableView!
//数据库引用
var dbRef:FIRDatabaseReference?
var places = [Places]()
private var loadedLabels = [String:String]()
private var loadedRatings = [String:Int]
//处理程序
var句柄:FIRDatabaseHandle?
$ b重写func viewDidLoad(){
super.viewDidLoad()
dbRef = FIRDatabase.database()。reference()
//将数据加载到单元格。
loadData()
}
private func loadData(){
let place =Dio Con Dio
观察(.childAdded,在:
(快照)
let label = snapshot.value as!String
self.updatePlace(snapshot .key,label:label)
))
dbRef!.child(place +/ rating)。 rating = snapshot.value as!Int
self.updatePlace(snapshot.key,rating:rating)
})
}
private func updatePlace(_ key:String,label:String?= nil,rating:Int?= nil){
如果let label = label {
loadedLabels [key] = label
}
如果让ratin g = rating {
loadedRatings [key] = rating
}
guard let label = loadedLabels [key],let rating = loadedRatings [key] else {
return
}
如果let place = Places(name:label,rating:rating){
places.append(place)
placesTableView.reloadData()
}
}
}
为您的新数据库模式,尝试使用这个:
private func loadData(){
dbRef!.observe(.childAdded){
(placeSnapshot) in
print(Adding place \(placeSnapshot.key)...)
//加载placeLabel列表上的每个标签。
让labels = placeSnapshot.childSnapshot(forPath:placeLabel)
for(key,label)以labels.value as! [String:String] {
self.updatePlace(key,label:label)
}
//载入rating列表中的每个等级。
让rating = placeSnapshot.childSnapshot(forPath:rating)
为(key,rating)的ratings.value为! [String:Int] {
self.updatePlace(key,rating:rating)
}
}
}
$ b $ updatePlace
函数简单地 给定地点的标签和评级属性,这是由 loadData
。$ b
删除地点单独加载的。当然,上面的代码不会处理地方删除。为此,您还需要监听 childRemoved
事件。
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()
}
}
}
(This question is a follow up from this one.)
For your new database schema, try this instead:
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)
}
}
}
The updatePlace
function simply groups the label and rating properties, of a given place, that were separately loaded by loadData
.
Places removal. Of course, the above code won't handle place removal. To do so, you will need to listen for the childRemoved
event as well.
这篇关于在函数之外获得一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!