【发布时间】:2020-10-01 17:37:57
【问题描述】:
我在我的应用程序中存储了一个名为 month hours 的值,用于跟踪用户使用应用程序的小时数并将其显示在一行文本中。如果文本是 Swift UI 中堆栈的一部分,但我无法弄清楚如何在查询信息后更新文本我已经尝试了很多方法来使这项工作从结构到类到使用 @状态。
这只是我尝试过的最新方法,但如果有人可以提供帮助,将不胜感激。
let db = Firestore.firestore()
class Month {
var monthHours = "0"
func getMonthHours() {
db.addSnapshotListener(. //Im removing the actual query part to keep that private but the print statement below confirms the query is not the issue.
{ (docSnapShot, err) in
if let e = err {
print("There was an error retrieving the monthly hours:\n\(e.localizedDescription)")
} else {
let data = docSnapShot?.data()
if let h = data?[K.FStore.monthHoursField] as? Double {
self.monthHours = String(h.rounded())
print("These are the hours:\n\(self.monthHours)")
}
}
})
}
func getMonth() -> String {
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MMMM yyyy"
let result = formatter.string(from: date)
return result
}
init() {
getMonthHours()
}
}
struct ChartView : View {
@State private var month = Month()
//Struct variables
var body : some View {
ZStack {
Color(UIColor(named: K.BrandColors.grey)!).edgesIgnoringSafeArea(.all)
VStack {
Text("HOURS THIS MONTH \(month.monthHours)")
.font(.system(size: 18))
.fontWeight(.heavy)
}
}
}
【问题讨论】:
标签: swift google-cloud-firestore swiftui updates