Swift表达过于复杂,无法在合理的时间内解决 [英] Swift expression was too complex to be solved in reasonable time
问题描述
在Xcode中编译项目时出现错误,它说:
I'm having an error when compiling a project in Xcode, it says:
表达太复杂,无法合理解决时间;考虑将表达式分成不同的子表达式
Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions
代码如下:
static func random(min: CGFloat, max: CGFloat) -> CGFloat {
return CGFloat(Float(arc4random()/0xFFFFFFFF) * (max - min) + min)
}
推荐答案
为什么不通过将表达式分解为两个子表达式来降低编译器的复杂性?
Why not reduce the complexity for the compiler by breaking the expression down into two sub-expressions?
static func random(min: CGFloat, max: CGFloat) -> CGFloat {
let rand = CGFloat(arc4random()/0xFFFFFFFF)
return (rand * (max - min) + min)
}
您还可以使用 UINT32_MAX
(或更多 Swifty UInt32。 max
或 .max
)代替 0xFFFFFFFF
来提高可读性。如果我还记得, 0xFFFFFFFF
是< stdint.h> <中定义的无符号32位整数最大值的十六进制值。 / code>标头。
You can also use UINT32_MAX
(or the more "Swifty" UInt32.max
or .max
) in place of 0xFFFFFFFF
to improve readability. If I recall, 0xFFFFFFFF
is the hex value of the max value of an unsigned 32-bit Integer as defined in the <stdint.h>
header.
#define UINT32_MAX 0xffffffff /* 4294967295U */
这篇关于Swift表达过于复杂,无法在合理的时间内解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!