Ios 为什么这个print()每次触发两次?
我的打印功能在我的代码中多次运行,下面是代码和运行结果:Ios 为什么这个print()每次触发两次?,ios,swift,swiftui,Ios,Swift,Swiftui,我的打印功能在我的代码中多次运行,下面是代码和运行结果: struct ContentView: View { let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() @State private var counter = 0 var body: some View { Text("Hello, World!") .onReceive(timer) { time in
struct ContentView: View {
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
@State private var counter = 0
var body: some View {
Text("Hello, World!")
.onReceive(timer) { time in
if self.counter == 5 {
self.timer.upstream.connect().cancel()
}
else {
self.counter += 1
print("The time is now \(time)")
}
}
}
}
在这段时间内,您的视图可能会被实例化多次。在这里可以正常工作。使用Xcode 11.2/iOS 13.2进行测试