2015-04-22 14 views
14

Quindi ho questo piccolo algoritmo nel mio progetto Xcode e non funziona più - mi sta dicendo che non posso aggiungere un numero a un altro numero, non importa quello che cerco.Uso ambiguo dell'operatore "+"

Nota:
Tutto stava funzionando perfettamente prima ho cambiato il mio obiettivo di iOS 7.0.
Non so se questo ha qualcosa a che fare con esso, ma anche quando sono passato a iOS 8 mi ha dato un errore e la mia build non è riuscita.

Codice:

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 
} 

risposta

17

Il problema è che delayCounter è un Int, ma arc4random_uniform restituisce un UInt32. Si sia necessario dichiarare delayCounter come UInt32:

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

o convertire il risultato di arc4random a un Int:

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

Grazie per il vostro aiuto! –

4

La funzione arc4random_uniform restituisce un UInt32, ma delayCounter è di tipo Int. Non esiste una definizione di operatore per + in Swift, che accetta come parametri uno UInt32 e Int. Quindi Swift non sa cosa fare con questa occorrenza dell'operatore + - è ambiguous.

Pertanto dovrete lanciare il UInt32 a un Int prima, utilizzando un inizializzatore esistente di Int, che prende un UInt32 come parametro:

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 

Un approccio diverso sarebbe di dichiarare delayCounter come un UInt32: funzione di

let delayCounter: UInt32 = 100000 
1

arc4random_uniform ritorna UInt32. È necessario convertirlo in Int

dichiarazione Funzione
func arc4random_uniform(_: UInt32) -> UInt32

Soluzione:
lasciare redDelay: NSTimeInterval = NSTimeInterval (Int (arc4random_uniform (100000)) + delayCounter)/30000

+0

Grazie per il vostro aiuto! –