无法在合理的时间内解决

无法在合理的时间内解决

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

问题描述

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

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

代码如下:

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> 标头。

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表达过于复杂,无法在合理的时间内解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:53