Reputation: 137
I want to read the restaurant
names Burger King
, Eating Point
using swift.
How it can be possible, Can anyone help me please this is for my final project need to submit in 1 week please i'm losing hope with this
ref = Database.database().reference()
ref?.child("Restaurants").observe(.childAdded, with: { (snapshot) in
let rest = snapshot.value as? String
if let actualPost = rest {
self.restList.append(actualPost)
print("list of rest ", self.restList)
self.restTableView.reloadData()
}
})
Upvotes: 3
Views: 1301
Reputation: 363
To get only restaurant names use below code.
dbReference = Database.database().reference()
dbReference?.child("Restaurants").observeSingleEvent(of: .value, with: {(snapshot) in
for rest in snapshot.children.allObjects as! [DataSnapshot] {
print("Restaurant Name:\(rest.key)")
}
})
And to pass all data using StoryboardId use below code.
dbReference = Database.database().reference()
dbReference?.child("Restaurants").observeSingleEvent(of: .value, with: {(snapshot) in
for rest in snapshot.children.allObjects as! [DataSnapshot] {
print("Restaurant Data:\(rest)")
}
})
Put this in your And declare data variable in destinationController like below:
var data = [DataSnapshot]()
You have to performsegue from didselectRowAt like this.
performSegue(withIdentifier: "segue", sender: self)
And you can pass the data of selected item from the below function.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let index = CategorytableView.indexPathForSelectedRow
let indexNumber = index?.row
print(indexNumber!)
let VC = segue.destination as! DestinationVC
VC.data = [rest] . //You can pass here entire data of selected row.
}
Upvotes: 1
Reputation: 13557
Just use below code to get parent node name.
ref = Database.database().reference()
ref?.child("Restaurants").observe(.childAdded, with: { (snapshot) in
let rest = snapshot.value as? String
// snapshot.key is give you parent node name
print(snapshot.key)
}
})
Upvotes: 2