【发布时间】:2020-06-25 17:15:17
【问题描述】:
我正在尝试从我的 firebase 实时数据库中检索数据,但我被以下代码困住了如何正确处理它。我有一个表查看的类别列表,在某些类别选择某个类别时,我想向@ 987654321从数据库显示到视图中的@ 987654321键值。有可能吗?
这是我的代码:
struct DailyData: Identifiable {
let id = UUID()
let name: String
let model: String
let text: String
}
extension DailyData {
static func all() -> [DailyData] {
return [
DailyData(name: "Beginning", model: "", text: ""),
DailyData(name: "Midnight Time", model: "Matins", text: ""),
DailyData(name: "Morning Time", model: "Lauds", text: ""),
DailyData(name: "Sunrise Time", model: "Prime", text: ""),
DailyData(name: "Third Hour", model: "Terce", text: ""),
DailyData(name: "Sixth Hour", model: "Sext", text: ""),
DailyData(name: "Ninth Hour", model: "None", text: ""),
DailyData(name: "Evening Time", model: "Vespers", text: ""),
DailyData(name: "Peace Time", model: "", text: ""),
DailyData(name: "Night Time", model: "Compline", text: ""),
]
}
}
Firebase 调用:
struct LiturgyData: Codable, Identifiable {
var id: Int
var title: String
var data: String
}
public class LiturgyFetcher: ObservableObject {
@Published var sermons = [LiturgyData]()
init(){
load()
}
func load() {
let url = URL(string: "https://MYPRIVATEURL.firebaseio.com/liturgyOfHours/.json")!
URLSession.shared.dataTask(with: url) {(data,response,error) in
do {
if let d = data {
let decodedLists = try JSONDecoder().decode([LiturgyData].self, from: d)
DispatchQueue.main.async {
print("finished getting services")
self.sermons = decodedLists
}
}else {
print("No Data")
}
} catch {
print ("Error")
}
}.resume()
}
}
来自 URL 的 JSON 结果:
[
{
"data": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
"id": 0,
"title": "Beginning"
},
{
"data": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
"id": 2,
"title": "Morning Time (Lauds)"
}
]
查看:
struct DailyWorshipView: View {
@State private var model = DailyData.all()
var body: some View {
NavigationView {
List {
ForEach(model) { dailydata in
LiturgyCell(dailydata: dailydata)
}
}
.navigationBarTitle(Text("Hours"))
}
}
#if DEBUG
struct DailyWorshipView_Previews: PreviewProvider {
static var previews: some View {
DailyWorshipView()
}
}
#endif
struct LiturgyCell: View {
let dailydata: DailyData
var body: some View {
NavigationLink(destination: LiturgyDetail(liturgy: dailydata)) {
HStack {
Text(dailydata.name)
}
}
}
}
}
【问题讨论】:
-
您面临的实际问题是什么?
-
当我选择类别以将数据键值显示到我的视图时,未获取键值“数据”。
-
你能把你的view的代码也加进去吗?
-
我添加了视图
-
您在哪里以及如何将您的类别与下载的数据合并?
标签: ios swift firebase firebase-realtime-database swiftui