You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI实现每日午夜Bool值自动重置及报错解决求助

解决每日午夜自动切换Bool值的问题及代码错误修复

错误原因分析

1. Type '()' cannot conform to 'View'

你的body代码存在结构不完整问题:NavigationView和VStack未闭合,且if语句块内直接执行赋值操作(navig2 = true),这不属于合法的View内容,导致SwiftUI无法识别有效的视图结构。

2. generic struct 'ObservedObject' requires that 'ContentView.Daily' conform to 'ObservableObject'

Daily类已实现ObservableObject,但你用@State存储它的实例。@State仅适用于值类型(如结构体、基本类型),对于遵循ObservableObject的引用类型,需使用@StateObject(iOS 14+,用于视图内部初始化)或@ObservedObject(用于接收父视图传递的实例)。

修正后的完整实现

import SwiftUI
import Foundation

class Daily: ObservableObject {
    @Published var timedBool: Bool = false
    private var timer: Timer?
    
    init() {
        scheduleDailyReset()
        // 可选需求:如果当前已过当日午夜,直接设为true
        // let now = Date()
        // let startOfDay = Calendar.current.startOfDay(for: now)
        // timedBool = now > startOfDay
    }
    
    private func scheduleDailyReset() {
        let calendar = Calendar.current
        guard let nextMidnight = calendar.date(byAdding: .day, value: 1, to: calendar.startOfDay(for: Date())) else {
            return
        }
        let timeInterval = nextMidnight.timeIntervalSinceNow
        timer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: false) { [weak self] _ in
            self?.timedBool = true
            self?.scheduleDailyReset() // 调度下一天的定时器
        }
    }
    
    deinit {
        timer?.invalidate() // 销毁定时器,避免内存泄漏
    }
}

struct ContentView: View {
    @StateObject private var dailyManager = Daily()
    @State private var navig2 = false
    
    var body: some View {
        NavigationView {
            VStack {
                Text(dailyManager.timedBool ? "已到午夜" : "未到午夜")
                    .font(.title)
                
                NavigationLink("跳转页面", destination: SecondView(), isActive: $navig2)
            }
            .padding()
            .onChange(of: dailyManager.timedBool) { newValue in
                if newValue {
                    navig2 = true
                }
            }
        }
    }
}

struct SecondView: View {
    var body: some View {
        Text("跳转后的页面")
            .font(.title)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

关键修正点

  • 用@StateObject替代@State管理Daily实例,确保SwiftUI能监听@Published属性的变化。
  • 重构定时器逻辑,使用闭包形式创建定时器并通过[weak self]避免循环引用。
  • 完善视图结构,闭合所有容器,使用.onChange监听timedBool变化来处理导航逻辑,而非在视图结构内直接赋值。
  • 添加deinit方法销毁定时器,防止内存泄漏。

内容的提问来源于stack exchange,提问作者Dana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 20:25:02