【发布时间】:2018-04-30 13:28:30
【问题描述】:
我已经尝试了五天了,但没有运气。
这是我的 Firebase 快照:
Snap (Channels) {
HiruFM = {
image = "hiru_fm_logo.png";
title = "Hiru FM";
};
SirasaFM = {
image = "sirasa_fm_logo.png";
title = SirasaFM;
};
}
以下是我尝试过的:
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference().child("Channels")
ref.observeSingleEvent(of: .value) { (snap) in
print(snap) // checking if the snap grabs the data (confirmed)
guard var data = snap.value as? Dictionary<String, Any> else {return}
let channeTitle = data["title"] as? String ?? "No title"
print(channeTitle) // prints "No title" (this is the problem)
let channelImage = data["image"] as? String ?? "No image"
print(channelImage) // prints "No image"
let myData = Channels(title: channeTitle, image: channelImage)
self.channels.append(myData)
}
}
【问题讨论】:
-
data中存在哪些键? -
只有字符串值
-
这不是我问的。您正在尝试访问键“标题”和“图像”,但它们似乎不存在。实际存在哪些键?
-
它在频道 > 子类别中。我可以专门深入研究这些类别,但是如果我有 100 个类别的快照(频道){ HiruFM = { image = "hiru_fm_logo.png";标题 = "Hiru FM"; }; SirasaFM = { image = "sirasa_fm_logo.png";标题=SirasaFM; }; }
-
你的问题。你有一本字典。
标签: json swift database firebase