Saturday, 15 May 2010

ios - Out of Bound Error-Synchronising AVaudioPlayer current time with a variable -



ios - Out of Bound Error-Synchronising AVaudioPlayer current time with a variable -

i have method keeps updating current time of audioplayer. in same method have variable gets incremented whenever current audioplayer time value gets equal time value stored in array. using next code works fine till 5th value app crashed , give out of bound index array error. not sure doing wrong. whole thought highlight row @ index path stored in variable x , increment highlighting after times passed stored in timearray

a variable defined @ interface

nsinteger x; nsarray *timearray;

check play time method keeps updating every 0.1s nstimer

- (void) checkplaybacktime:(nstimer *)thetimer { //method gets called every 0.1s double time=audioplayer.currenttime; double currentnumber = [((nsnumber*)[timearray objectatindex:x]) doublevalue];//gives values stored in array if (time>0 && time<currentnumber){ //i used method increment x nsinteger*a =&x; [self highlightcell:a]; } if (time>=currentnumber && time< 27.00){ nsinteger*b =&x; [self highlightcell:b]; x++; } }

let's suppose timearray contains 5 objects. index of lastly object in array x = 4. if increment x 5, -objectatindex: seek 6th object array, cannot work due array containing 5 objects - index hence out of bounds of array (as indexes 0..4 valid values x).

to avoid such situations, should check index within current bounds of array before trying access array element:

double currentnumber = 0.; if (x < [timearray count]) { currentnumber = [[timearray objectatindex:x] doublevalue]; }

ios objective-c nsarray avaudioplayer indexoutofboundsexception

No comments:

Post a Comment