快速施法导致应用程序崩溃

时间:2022-12-05 16:03:10

I've been having this odd error in a recent swift program of mine. It involves random occurrences, and to simulate this I assign an event 'odds,' then generate two random numbers (using these odds), and if the numbers are the same then the action occurs. But the program crashes, inexplicably, in the generation part. The only explanation I can think of is the overabundance of casting required, but I'm not sure why it only crashes once in a while. I'd appreciate any insight as to why the casting crashes and any suggestions for what to do to avoid such excessive casting.
My image shows the code and the error, and the code below is a generalization of my code. Crash Error

我最近在一个swift项目中犯了这个奇怪的错误。它涉及随机事件,为了模拟这一点,我分配了一个事件“odds”,然后生成两个随机数(使用这些odds),如果这些数字是相同的,那么操作就会发生。但令人费解的是,这个程序在生成部分崩溃了。我能想到的唯一的解释是需要过多的铸件,但我不确定为什么它只会在一段时间内发生碰撞。我想知道为什么选角会失败,以及如何避免选角过多的建议。我的映像显示了代码和错误,下面的代码是代码的概括。崩溃的错误

let rand = [Int(arc4random_uniform(UInt32(someInt))), Int(arc4random_uniform(UInt32(someInt)))]
if (rand[0] == rand[1]) {
    executeAction()
}

2 个解决方案

#1


1  

This occurs because your integer variable shootOdds, at some point, takes a negative value (or: less plausible, a value larger than 4,294,967,295), causing runtime error for cast to unsigned integer, UInt32(someInt). You can avoid this by making sure, prior to the let rand = ... row, that shootOdds >= 0 (or in your code example above, someInt >= 0), or that you number is not larger than the upper limit for UInt32.

之所以会出现这种情况,是因为您的整数变量shootOdds在某些时候会取一个负值(或者:不太可信的值,一个大于4,294,967,295的值),从而导致强制转换为无符号整数UInt32(someInt)的运行时错误。您可以通过确保在let rand =…行,射手座>= 0(或者在上面的代码示例中,someInt >= 0),或者您的编号不大于UInt32的上限。

Hence note that the error is not associated with the rand function, but specifically the negative integer cast to unsigned integer.

因此,请注意,该错误与rand函数无关,但具体地说,是将负整数转换为无符号整数。

E.g., try the following example in your playground, to assert you get the same runtime error:

例句:在你的操场上试试下面的例子,你会得到同样的运行时错误:

let a = -1
let b = UInt32(a)

#2


0  

Due to faulty code, my odds were increasing exponentially and eventually too large to be contained in UInt32... and thus the error. Thanks for the help!

由于错误的代码,我的几率呈指数级增长,最终太大而无法包含在UInt32中……因此错误。谢谢你的帮助!

#1


1  

This occurs because your integer variable shootOdds, at some point, takes a negative value (or: less plausible, a value larger than 4,294,967,295), causing runtime error for cast to unsigned integer, UInt32(someInt). You can avoid this by making sure, prior to the let rand = ... row, that shootOdds >= 0 (or in your code example above, someInt >= 0), or that you number is not larger than the upper limit for UInt32.

之所以会出现这种情况,是因为您的整数变量shootOdds在某些时候会取一个负值(或者:不太可信的值,一个大于4,294,967,295的值),从而导致强制转换为无符号整数UInt32(someInt)的运行时错误。您可以通过确保在let rand =…行,射手座>= 0(或者在上面的代码示例中,someInt >= 0),或者您的编号不大于UInt32的上限。

Hence note that the error is not associated with the rand function, but specifically the negative integer cast to unsigned integer.

因此,请注意,该错误与rand函数无关,但具体地说,是将负整数转换为无符号整数。

E.g., try the following example in your playground, to assert you get the same runtime error:

例句:在你的操场上试试下面的例子,你会得到同样的运行时错误:

let a = -1
let b = UInt32(a)

#2


0  

Due to faulty code, my odds were increasing exponentially and eventually too large to be contained in UInt32... and thus the error. Thanks for the help!

由于错误的代码,我的几率呈指数级增长,最终太大而无法包含在UInt32中……因此错误。谢谢你的帮助!