我想要一个布尔值,每天午夜都变成真,有谁知道我怎么能做到这一点.这是我迄今为止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.

推荐答案

你的计时器没问题.

你的代码中有几个问题:

  1. class Daily个代码移到顶层.这将删除@ObservableObject错误.

  2. @State var daily更改为@StateObject.这将使@ObservableObject正确更新.

  3. 不要将函数getBool()@State一起使用.直接使用@Published var timedbool,就像daily.timedbool一样.

  4. 在Body navig2 = true行中,您返回了一个Bool,您应该在其中返回一个View.你需要在这里放一个视角.

以下是我的改变:

import SwiftUI

class Daily: ObservableObject {
    @Published var timedbool: Bool = false
    private var timer: Timer?
    
    init() {
        scheduleDailyReset()
    }
    private func scheduleDailyReset() {
        let calendar = Calendar.current
        guard let tomorrow = calendar.date(byAdding: .day, value: 1, to: calendar.startOfDay(for: .now)) else {
//        guard let tomorrow = calendar.date(byAdding: .second, value: 5, to: .now) else { // use this instead to test
            print("There's no tomorrow.")
            return
        }
        
        let timeInterval = tomorrow.timeIntervalSinceNow
        timer = Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector: #selector(setBool), userInfo: nil, repeats: false)
        print("timer: \(timer?.fireDate)")
    }
    
    @objc func setBool() {
        timedbool = true
        scheduleDailyReset()
    }
}

struct ContentView: View {
    @StateObject var daily = Daily()
    
    var body: some View {
        VStack {
            Button("Confirm") {
                daily.timedbool = false  // set @Published var
            }
            .disabled(!daily.timedbool) // use @Published var
        }
        .padding()
        .onChange(of: daily.timedbool) { bool in // use @Published var
            print("timed bool set to \(bool)")
        }
    }
}

请注意,带有按钮的视图只是如何在视图中获取和设置timedbool的示例.

还有一个备用的Fire Date定义,因此您不必通宵查看它是否有效.

我更喜欢在这里用guard语句来展开可选的Date,以避免缩进.

Swift相关问答推荐

在Swift中initt()完成后运行一个函数

不使用`lockFocus`将两个重叠的NSImage转换为单个NSImage

Swift:iVar + Equatable上的协议约束

SwiftUI同心圆,通过逐渐出现来显示进度

仅使用@MainActor注释类的部分时的并发问题

查找数组中 ** 元素 ** 的属性的最小值和最大值

修改数组中每个类实例的属性

SwiftUI 组合问题:通过 AppEventsManager 在 ViewModel 之间共享数据

在 SwiftUI 视图中观察 UIViewRepresentable 的 @State var 变化

SwiftUI 无法在实时活动中显示 SpriteView

在 macOS (Swift) 上获取 BSD 驱动器名称的最佳方法是什么?

从 Int 到 String 的属性更改引发 SwiftUI 视图不更新

XCUITest 在 TearDown 期间随机失败-无法终止 com.bundle.id

如何使 SwiftUI Picker 换行文本

如何将回调传递给 View init

swift 如何从 Int 转换?到字符串

快速的 AES 加密

Xcode 6 中的嵌入式内容包含 Swift 代码构建设置有什么作用?

无法推断泛型参数的参数

更改在变量的 willSet 块中设置的值