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
    ...
   }
 }

The error I am getting is Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

I am a beginner swift programmer and am sure I am missing something obvious, was hoping for some insight. Thank you.

1 Answer 1

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 is an Uint32 and can't be negative. So either don't let it become negative or convert it to an Int

Sign up to request clarification or add additional context in comments.

5 Comments

In the OP code, time is impossible to be negative.
for time; time >= 0; time-- Won't this drop time to -1 at the end?
Correct, it should be time > 0.
Unless he needs to do something as well if the value is 0.
Also, it is possible that type inference for time1 corrects this to int automatically due to the -- at the end ;-). I think that's what happened in my playground.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.