Swift表达过于复杂,无法在合理的时间内解决 [英] Swift expression was too complex to be solved in reasonable time

查看:96
本文介绍了Swift表达过于复杂,无法在合理的时间内解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆