Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么这个print()每次触发两次?_Ios_Swift_Swiftui - Fatal编程技术网

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进行测试