I am trying to retrieve every object stored in Firebase. Not sure how to go ahead. The following is my code and snap.value printed. I need to retrieve String "cbc" and "cp24" as well as values for datasaved and url. "cbc" and "cp24" are names I entered when saving.
Ref_UsersBase.observeEventType(.Value, withBlock: { snapshot in
if let snapshots = snapshot.children.allObjects as? [FDataSnapshot] {
for snap in snapshots {
print(snap.value)
}
}
})
{
// Here is snap.value printed:
{
"-KGOzXm1lLtyS23cOR-G" = {
cp24 = {
datesaved = "Apr 27, 2016";
url = "https://www.google.ca/search?site=&source=hp&ei=H0chV6pfhKeOBJChpqAN&q=cp24+weather&oq=&gs_l=mobile-gws-hp.1.0.41l2.0.0.0.13343.1.1.0.1.1.0.368.368.3-1.1.0....0...1..64.mobile-gws-hp..0.1.6.2.JC6OI95nuYc";
};
};
"-KGOznhRN0J2OVlIogZ0" = {
cbc = {
datesaved = "Apr 27, 2016";
url = "http://www.cbc.ca/beta/news/canada/toronto";
};
};
}
Here's what I have done in a similar situation, I hope it helps you.
I declare a variable to store snapshot.value, a first array to store the keys at the first level ("-KGOzXm1lLtyS23cOR-G") and a second array to store the keys at second level ("cp24").
var receivedData = [String:[String:[String:String]]]()
var firstKeyArray = [String]()
var secondKeyArray = [String]()
I store the whole snapshot.value in receivedData.
Ref_UsersBase.observeEventType(.Value, withBlock: { snapshot in
self.receivedData = snapshot.value
})
Fill firstKeyArray and secondKeyArray
var i = 0
for(key,_) in receivedData{
firstKeyArray.append(key)
for(key,_) in receivedData[firstKeyArray[i]]!{
secondKeyArray.append(key)
}
i += 1
}
Finally you would access datesaved and url values like this
print(receivedData[firstKeyArray[0]]![secondKeyArray[0]]!["datesaved"]!)
//prints Apr 27
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.