2015-05-10 21 views
0
func randomroll() { 

    var time = arc4random_uniform(10) 

    while(time < 5) 
    { 
    time = arc4random_uniform(10) 
    } 

    for(var time1 = time; time1>=0; time1--) { //This is where I get the thread error 
    ... 
    } 
} 

我得到的錯誤是Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)斯威夫特For循環線程錯誤

我是初學者迅速程序員,我相信我缺少明顯的東西,希望對一些見解。謝謝。

回答

1
func randomroll() { 

    var time = Int(arc4random_uniform(10)) 

    while(time < 5) 
    { 
     time = Int(arc4random_uniform(10)) 
    } 

    for time; time >= 0; time-- { //This is where I get thread error 
     println(time) 
    } 
} 

randomroll() 

time是Uint32,不能是負數。因此,要麼不要讓它變成負數,要麼將其轉換爲Int

+0

在OP代碼中,「時間」不可能是負數。 – Mundi

+2

'時間;時間> = 0;時間 - '這會不會在最後把'時間'降到'-1'? – Eendje

+0

正確,應該是'time> 0'。 – Mundi