Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.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 模拟器冻结|迅捷,XCode_Ios_Swift_Xcode_Swiftui_Ios Simulator - Fatal编程技术网

Ios 模拟器冻结|迅捷,XCode

Ios 模拟器冻结|迅捷,XCode,ios,swift,xcode,swiftui,ios-simulator,Ios,Swift,Xcode,Swiftui,Ios Simulator,我的结果是模拟器冻结 我的预期结果是程序将随机数相加,直到我按下“非七”按钮时出现7为止 我的目标是完成这项挑战: *构建一个包含列表和下面三个按钮的UI 点击第一个按钮时: 向列表中添加一个随机整数(从1到10)。 如果添加到列表中的整数不是7,则继续向列表中添加随机整数(从1到10),直到将7添加到列表中。 点击第二个按钮时: 将列表中显示的所有整数增加1 点击第三个按钮时: 清除列表中的所有数字 这是指向节目录制的链接: 实际上,您从未在循环中更改randNumber

我的结果是模拟器冻结

我的预期结果是程序将随机数相加,直到我按下“非七”按钮时出现7为止

我的目标是完成这项挑战: *构建一个包含列表和下面三个按钮的UI

点击第一个按钮时:

向列表中添加一个随机整数(从1到10)。 如果添加到列表中的整数不是7,则继续向列表中添加随机整数(从1到10),直到将7添加到列表中。 点击第二个按钮时:

将列表中显示的所有整数增加1 点击第三个按钮时:

清除列表中的所有数字

这是指向节目录制的链接:


实际上,您从未在循环中更改
randNumber

             Button("Not Seven") {
                // You initialize the variable
                 var randNumber = 0
                // You set it to a random Int
                 randNumber = Int.random(in: 0...10)
                //Then you test forever if that random Int is 7. You have a 1 in 11 chance of an infinite loop, which is why the simulator hangs.
                 repeat{
                     numbers.append(randNumber)
                }while randNumber != 7
}
将按钮更改为:

            Button("Not Seven") {
                // No need to set it to 0 and then change it to a random Int prior to the loop.
                var randNumber = Int.random(in: 0...10)
                repeat {
                    numbers.append(randNumber)
                    //Change randNumber here to be a different random number.
                    randNumber = Int.random(in: 0...10)
                } while randNumber != 7
            }

您可能可以修改逻辑,使其不包含两个不同的'randNumber=Int.random(in:0…10)'语句,但我已经喝完咖啡,还没有考虑这一点。

我猜这与
重复
有关。如果您更改了它会发生什么?另外,为什么您的
PreviewProvider
中有两个
ContentView
s?请在根目录中将其更改为LazyVStack。UI跟不上,将其更改为Lazy意味着它将只显示您当前正在查看的内容..thx很多家伙
            Button("Not Seven") {
                // No need to set it to 0 and then change it to a random Int prior to the loop.
                var randNumber = Int.random(in: 0...10)
                repeat {
                    numbers.append(randNumber)
                    //Change randNumber here to be a different random number.
                    randNumber = Int.random(in: 0...10)
                } while randNumber != 7
            }