ios - NSMutableArray arrayWithCapacity issue -


i sorry if stupid question. if please leave comment , remove here code below:

numberedarray = [nsmutablearray arraywithcapacity:50]; nsuinteger randomnumber = arc4random() % [numberedarray count]; (int = 0; i<randomnumber; i++) {     // run code } 

}

now error of: thread 1: exc_arithmetic (code=exc_i386_div, subcode=0x0)

so best guess machine getting confused because telling array has capacity of 50 , pick 1 of slots randomly , perform code long loop less randomly picked number.

but want have computer run code based on randomly picked time interval.

this game code performs enemy moving left right based on randomly picked time interval. right approach? , if not should try instead? thanks.

capacity 50 doesn't mean array contains 50 elements. count 0. perform division 0 , that's why crashes. forget using arraywithcapacity: or initwithcapacity: because doesn't mean practically helpful.

update:

if need random number in range of 50 why not use just:

nsuinteger randomnumber = arc4random() % 50; 

and initialise array this:

numberedarray = [nsmutablearray array]; 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -