【发布时间】:2021-08-13 15:29:30
【问题描述】:
我正在尝试在列表中显示来自数组的数据列表,但无法正常工作,我在下面收到此错误
还有其他方法可以将数组中的数据显示到列表中吗?
谢谢:-)
错误:
Value of type 'Any' has no member 'title'
我的代码:
import SwiftUI
import FirebaseAuth
import FirebaseDatabase
import Foundation
struct goal: Identifiable {
var id: String
var description: String
var endDate: String
var category: String
}
struct ActiveGoalsView: View {
@State var goals = []
@State var ref = Database.database().reference()
@State private var multiSelection = Set<UUID>()
func getData() {
ref.child("users").child(Auth.auth().currentUser?.uid ?? "noid").child("goals").observeSingleEvent(of: .value) { snapshot in
for snap in snapshot.children {
let snap1 = snap as! DataSnapshot
let goalId = snap1.childSnapshot(forPath: "goalId").value
let description = snap1.childSnapshot(forPath: "description").value
let endDate = snap1.childSnapshot(forPath: "end_date").value
let category = snap1.childSnapshot(forPath: "category").value
goals.append(goal(id: goalId as! String, description: description as! String, endDate: endDate as! String, category: category as! String))
//print(snap1.childSnapshot(forPath: "goalId").value)
//print(snap)
print(goals)
}
}
}
var body: some View {
NavigationView {
ForEach(goals, id: \.self) {goal in
Text(goal.title)
}
List() {
Button(action: {getData()}, label: {
Text("Button")
})
/*
ForEach(goals, id: \.self) {goals in
HStack {
Button(action: {}, label: {
Text(goals)
})
}
}*/
}.navigationBarHidden(true)
}
}
}
struct ActiveGoalsView_Previews: PreviewProvider {
static var previews: some View {
ActiveGoalsView()
}
}
【问题讨论】:
标签: ios swift firebase swiftui