ios - 错误 : Instance method 'onReceive(_:perform:)' requires that 'Int' conform to 'Publisher'

标签 ios swift swiftui

我目前有一个 Text()根据 10 分钟计时器 ( 600 seconds ) 更改,格式为 9:59 , 9:58等等
下面的代码正在工作,但是现在我遇到了一个问题。我想在 540 seconds 执行一个操作( 9:00 ),所以我补充说:

if self.countdown.secondsLeft <= 540 && !codeSent {
    self.codeSent = true
}
然而,这给出了以下错误:
Modifying state during view update, this will cause undefined behavior.
因此,我想在我的 onReceive(self.countdown.secondsLeft) 中添加剩余的秒数。如下所示的方法 - 但 swift 不接受 self.countdown.secondsLeft因为它不是 @Published Int :
Instance method 'onReceive(_:perform:)' requires that 'Int' conform to 'Publisher'
然而,确实如此。
我的代码如下:
HomeView.swift
struct HomeView: View {
    @ObservedObject var waitingCountdown = CountDown(secondsLeft: 600)

    var body: some View {
            ZStack {
                WaitingTimer(countdown: self.waitingCountdown)
            }
    }
WaitingTimer.swift
struct WaitingTimer: View {
    @ObservedObject var countdown: CountDown
    @State var codeSent = false
    var body: some View {
        VStack{
            Text("\(timerText())")
                .font(.system(size: 28))
                .fontWeight(.bold)
                .foregroundColor(Color("Exit"))
                .frame(width: 80, height: 40)
                .padding(EdgeInsets(top: 3, leading: 10, bottom: 3, trailing: 10))
                .background(
                    Capsule()
                        .fill(Color.white)
                        .opacity(0.7)
                )
                .overlay(
                    Capsule()
                        .stroke(Color("Exit"), lineWidth: 2)
                )
                .onReceive(self.countdown.secondsLeft){
                    
                }
                .onAppear{
                    print("Start countdown")
                    self.countdown.start()
                }
                .onDisappear {
                    self.countdown.stop()
                }

        }.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
        .padding(.top, 30)
        .padding(.leading, 30)
    }
    
    func timerText() -> String{
        print("seconds left: \(self.countdown.secondsLeft) | codeSent: \(codeSent)")
        let minutes = Int( floor(Double(self.countdown.secondsLeft) / 60) )
        let _seconds = (self.countdown.secondsLeft) - (minutes * 60)
        if self.countdown.secondsLeft <= 540 && !codeSent {
            print("SECONDS LEFT: \(self.countdown.secondsLeft) | CODESENT: \(codeSent)")
            self.codeSent = true
        }
        // Show 9:09 instead of 9:9
        let seconds = _seconds < 1 ? "0\(_seconds)" : String(_seconds)
        print("Minutes \(minutes) Seconds left: \(seconds)")
        return "\(minutes):\(seconds)"
        
    }
}
CountDown.swift
class CountDown: ObservableObject {
    init(secondsLeft: Int){
        self.secondsLeft = secondsLeft
    }
    @Published var secondsLeft: Int
    var timer: Timer?
    func start(){
        print("start timer: \(self.secondsLeft)")
        self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true){ _ in
            self.secondsLeft -= 1
        }
    }
    func stop(){
        self.timer?.invalidate()
    
}
}
知道问题是什么吗?

最佳答案

@Zorgan 对于符合发布者错误的要求:

Instance method 'onReceive(_:perform:)' requires that 'Int' conform to 'Publisher'
在 onReceive 的 Published 属性上添加 $
onReceive(self.countdown.$secondsLeft)

关于ios - 错误 : Instance method 'onReceive(_:perform:)' requires that 'Int' conform to 'Publisher' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62976622/

相关文章:

ios - 停止/暂停 .runActionForever() SCNAction

ios - 文本包含表情符号时文本高度错误

ios - 如何使用 SwiftUI 扩展按钮的宽度

swift - 如何摆脱 SwiftUI TextFields 周围的蓝色选择边框?

ios - 如何从我的应用程序内部发送 APNS

ios - 是否有适用于 iOS 的 Android 的 ShowcaseView?

ios - 如何验证 Core Audio 中的音频幅度是否正确?

swift - 当尺寸设置为 500x500 像素时,调整生成 1000x1000 像素尺寸的大分辨率图像的尺寸

ios - Swift 膨胀 [UInt8] 无法使用 miniz lib 缩小

alignment - 如何在 SwiftUI 中对齐文本和图像的顶部