我想要一个布尔值,每天午夜都变成真,有谁知道我怎么能做到这一点.这是我迄今为止try 过的代码.我一直在犯这些错误:
类型‘()’不符合‘View’"
和
泛型 struct "ObservedObject"要求"ContentView.Daily"符合"ObservableObject"
import SwiftUI
import Foundation
struct ContentView: View {
class Daily: ObservableObject {
@Published var timedbool: Bool = false
private var timer: Timer?
init(){
scheduleDailyReset()
}
private func scheduleDailyReset(){
let calendar = Calendar.current
if let tomorrow = calendar.date(byAdding: .day, value: 1, to: calendar.startOfDay(for: Date())) {
let timeInterval = tomorrow.timeIntervalSinceNow
timer = Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector: #selector(setBool), userInfo: nil, repeats: false)
}
}
@objc func setBool() {
timedbool = true
scheduleDailyReset()
}
func getBool() -> Bool {
return timedbool
}
}
@State var daily = Daily()
@State var dailyreset: Bool = daily.getBool()
var body: some View {
NavigationView {
VStack{
if dailyreset{
navig2 = true
}
I tried to make a class with a function to do the daily reset 和 then another function to return the reset bool value but it won't conform to View.