Search code examples
iosswiftxcode6backwards-compatibility

Ambiguous use of operator "+"


So I have this little algorithm in my Xcode project and it no longer works - it's telling me that I can't add a number to another number, no matter what I try.

Note:
Everything was working perfectly before I changed my target to iOS 7.0.
I don't know if that has anything to do with it, but even when I switched it back to iOS 8 it gave me an error and my build failed.

Code:

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
}

Solution

  • The trouble is that delayCounter is an Int, but arc4random_uniform returns a UInt32. You either need to declare delayCounter as a UInt32:

    var delayCounter: UInt32 = 100000
    let redDelay = NSTimeInterval(arc4random_uniform(100000) + delayCounter) / 30000
    

    or convert the result of arc4random to an Int:

    var delayCounter:Int = 100000
    let redDelay = NSTimeInterval(Int(arc4random_uniform(100000)) + delayCounter) / 30000