我正在尝试检索存储在Firebase中的每个对象。不知道如何继续。以下是我的代码和snap.value打印。我需要检索字符串“cbc”和“cp24”以及datasaved和url的值。 “cbc”和“cp24”是我保存时输入的名称。
Ref_UsersBase.observeEventType(.Value, withBlock: { snapshot in
if let snapshots = snapshot.children.allObjects as? [FDataSnapshot] {
for snap in snapshots {
print(snap.value)
}
}
})
{
//这是snap.value打印:
{
"-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";
};
};
}
答案 0 :(得分:1)
这是我在类似情况下所做的事情,我希望它对你有帮助。
我声明了一个变量来存储snapshot.value,第一个数组用于存储第一级的键(“-KGOzXm1lLtyS23cOR-G”),第二个数组用于存储第二级的键(“cp24”)。
var receivedData = [String:[String:[String:String]]]()
var firstKeyArray = [String]()
var secondKeyArray = [String]()
我将整个snapshot.value存储在receivedData中。
Ref_UsersBase.observeEventType(.Value, withBlock: { snapshot in
self.receivedData = snapshot.value
})
填写firstKeyArray和secondKeyArray
var i = 0
for(key,_) in receivedData{
firstKeyArray.append(key)
for(key,_) in receivedData[firstKeyArray[i]]!{
secondKeyArray.append(key)
}
i += 1
}
最后,您可以像这样访问dateaved和url值
print(receivedData[firstKeyArray[0]]![secondKeyArray[0]]!["datesaved"]!)
//prints Apr 27