iphone - how do I create fresh NSMutableArray? -


i have nsmutablearray lasts during session. create this

nsmutablearray *temp = [[nsmutablearray alloc] initwithcapacity:10];     [self setscorearray:temp];     [temp release]; 

problem when go check each index i'm getting array outofbounds error

nsnumber *previousscore = [[self scorearray] objectatindex:[self quiznum]];     if ( previousscore != nil )      {         [self clearquizbtns];         nsinteger previousscorevalue = [previousscore integervalue];         [self selectbuttonattag:previousscorevalue];     }else {         [self clearquizbtns];     } 

i've read in other posts initwithcapacity doesn't create array. can populate array initially? in advance.

two ways:

first: initiate array default values of nsnull class

nsmutablearray *temp = [[nsmutablearray alloc] initwithcapacity:10]; (int = 0 ; < 10 ; i++) {     [temp insertobject:[nsnull null] atindex:i]; }  [self setscorearray:temp]; [temp release]; 

and check: if object kind of nsnull class means never set before

id previousscore = [[self scorearray] objectatindex:[self quiznum]]; if (![previousscore iskindofclass:[nsnull class]])  {     [self clearquizbtns];     nsinteger previousscorevalue = [(nsnumber *)previousscore integervalue];     [self selectbuttonattag:previousscorevalue]; }else {     [self clearquizbtns]; } 

second: store scores in nsmutabledictionary , use nsnumber's keys

// scoredictionary property of nsmutabledictionary class must declared in self  nsnumber *previousscore = [self.scoredictionary objectforkey:[nsnumber numberwithint:[self quiznum]]]; if (previousscore != nil)  {     [self clearquizbtns];     nsinteger previousscorevalue = [previousscore integervalue];     [self selectbuttonattag:previousscorevalue]; }else {     [self clearquizbtns]; } 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -