xcode Swift 表达式太复杂,无法在合理的时间内解决

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33794574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:13:33  来源:igfitidea点击:

Swift expression was too complex to be solved in reasonable time

xcodeswiftarc4randomcgfloat

提问by lagro23

I'm having an error when compiling a project in Xcode, it says:

在 Xcode 中编译项目时出现错误,它说:

Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions

表达式太复杂,无法在合理的时间内解决;考虑将表达式分解为不同的子表达式

here's the code:

这是代码:

static func random(min: CGFloat, max: CGFloat) -> CGFloat {
    return CGFloat(Float(arc4random()/0xFFFFFFFF) * (max - min) + min)
}

采纳答案by JAL

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)
}

You can also use UINT32_MAX(or the more "Swifty" UInt32.maxor .max) in place of 0xFFFFFFFFto improve readability. If I recall, 0xFFFFFFFFis the hex value of the max value of an unsigned 32-bit Integer as defined in the <stdint.h>header.

您还可以使用UINT32_MAX(或更“Swifty”UInt32.max.max)代替0xFFFFFFFF来提高可读性。如果我记得,0xFFFFFFFF是在<stdint.h>标题中定义的无符号 32 位整数的最大值的十六进制值。

#define UINT32_MAX 0xffffffff  /* 4294967295U */