模糊地使用运算符“+”

时间:2015-04-22 12:13:22

标签: ios swift xcode6 backwards-compatibility

所以我在我的Xcode项目中有这个小算法,它不再有效 - 它告诉我,无论我尝试什么,我都无法将数字添加到另一个数字。

注意:
在将目标更改为 iOS 7.0 之前,一切都运行良好 我不知道这与它有什么关系,但即使我把它切换回 iOS 8 它也给了我一个错误,我的构建失败了。

代码:

var delayCounter = 100000

for loop in 0...loopNumber {
    let redDelay: NSTimeInterval = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 30000

    let blueDelay: NSTimeInterval = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 30000

    let yellowDelay: NSTimeInterval = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 30000

    let greenDelay: NSTimeInterval = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 30000
}

3 个答案:

答案 0 :(得分:17)

问题是Intarc4random_uniform,但UInt32会返回delayCounter。您需要将UInt32声明为var delayCounter: UInt32 = 100000 let redDelay = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 30000

arc4random

或将Int的结果转换为var delayCounter:Int = 100000 let redDelay = NSTimeInterval(Int(arc4random_uniform(100000)) + delayCounter) / 30000

contentFrame = (By.ID, 'testcore')

答案 1 :(得分:4)

函数arc4random_uniform返回UInt32,但delayCounter的类型为Int。 Swift中没有+的运算符定义,但它以UInt32Int为参数。因此,Swift不知道如何处理+运算符的出现 - 它是ambiguous

因此,您必须首先使用UInt32的现有初始值设定项IntInt转换为UInt32,其中let redDelay: NSTimeInterval = NSTimeInterval(Int(arc4random_uniform(100000)) + delayCounter) / 30000 let blueDelay: NSTimeInterval = NSTimeInterval(Int(arc4random_uniform(100000)) + delayCounter) / 30000 let yellowDelay: NSTimeInterval = NSTimeInterval(Int(arc4random_uniform(100000)) + delayCounter) / 30000 let greenDelay: NSTimeInterval = NSTimeInterval(Int(arc4random_uniform(100000)) + delayCounter) / 30000 为其参数:< / p>

delayCounter

另一种方法是将UInt32声明为let delayCounter: UInt32 = 100000

arc4random_uniform

答案 2 :(得分:1)

UInt32函数返回Int。您需要将其转换为func arc4random_uniform(_: UInt32) -> UInt32

功能声明
&#216

解决方案:
    让redDelay:NSTimeInterval = NSTimeInterval(Int(arc4random_uniform(100000))+ delayCounter)/ 30000