swift 错误:示例方法“onReceive(_:perform:)”要求“Int”符合“Publisher”

fhg3lkii  于 2023-03-07  发布在  Swift
关注(0)|答案(2)|浏览(140)

我目前有一个Text(),它根据10分钟的计时器(600 seconds)而变化,格式为9:599:58等。
下面的代码可以正常工作,但是现在我遇到了一个问题。我想在540 seconds9: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,因为它不是@PublishedInt

Instance method 'onReceive(_:perform:)' requires that 'Int' conform to 'Publisher'

但是,它是。
我的代码如下:

    • 主页视图. swift**
struct HomeView: View {
    @ObservedObject var waitingCountdown = CountDown(secondsLeft: 600)

    var body: some View {
            ZStack {
                WaitingTimer(countdown: self.waitingCountdown)
            }
    }
}
    • 等待计时器. 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)"
        
    }
}
    • 倒计时,快速**
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()
    }
}

你知道问题出在哪里吗?

xj3cbfub

xj3cbfub1#

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

Instance method 'onReceive(_:perform:)' requires that 'Int' conform to 'Publisher'

在onReceive中的Published属性上添加$

onReceive(self.countdown.$secondsLeft)
vqlkdk9b

vqlkdk9b2#

然而,这会产生以下误差:
请尝试以下操作(通常可以修复此类错误)

if self.countdown.secondsLeft <= 540 && !codeSent {
    DispatchQueue.main.async { // << update state async
       self.codeSent = true
    }
}

相关问题