Ios 使用变量同步AVaudioPlayer当前时间时发生越界错误

Ios 使用变量同步AVaudioPlayer当前时间时发生越界错误,ios,objective-c,nsarray,avaudioplayer,indexoutofboundsexception,Ios,Objective C,Nsarray,Avaudioplayer,Indexoutofboundsexception,我有一种方法可以不断更新audioPlayer的当前时间。在相同的方法中,我有一个变量,每当当前audioPlayer时间值等于存储在数组中的时间值时,该变量就会递增。我正在使用以下代码,该代码在第5个值之前工作正常,但随后应用程序崩溃,并给出超出范围的索引数组错误。我不确定我做错了什么。整个想法是高亮显示存储在变量x中的索引路径上的一行,并在存储在timeArray中的特定时间过后增加高亮显示 在接口处定义的变量 NSInteger x; NSArray *timeArray; 检查回放时间

我有一种方法可以不断更新audioPlayer的当前时间。在相同的方法中,我有一个变量,每当当前audioPlayer时间值等于存储在数组中的时间值时,该变量就会递增。我正在使用以下代码,该代码在第5个值之前工作正常,但随后应用程序崩溃,并给出超出范围的索引数组错误。我不确定我做错了什么。整个想法是高亮显示存储在变量x中的索引路径上的一行,并在存储在timeArray中的特定时间过后增加高亮显示

在接口处定义的变量

NSInteger x;
NSArray *timeArray;
检查回放时间方法由NSTimer每0.1s更新一次

- (void) checkPlaybackTime:(NSTimer *)thetimer {  //method gets called every 0.1s
    double time=audioPlayer.currentTime;
    double currentNumber = [((NSNumber*)[timeArray objectAtIndex:x]) doubleValue];//gives the values stored in Array
      if (time>0 && time<currentNumber){      //i used this method to increment x
        NSInteger*a =&x;
        [self highlightcell:a];
          }

      if (time>=currentNumber && time< 27.00){
        NSInteger*b =&x;
        [self highlightcell:b];
        x++;
       }
}
-(void)checkPlaybackTime:(NSTimer*)timer{//方法每0.1秒调用一次
双倍时间=audioPlayer.currentTime;
double currentNumber=[((NSNumber*)[timeArray objectAtIndex:x])doubleValue];//给出存储在数组中的值
如果(时间>0&&time=currentNumber&&time<27.00){
NSInteger*b=&x;
[自发光电池:b];
x++;
}
}

假设您的
时间数组包含5个对象。数组中最后一个对象的索引将是
x=4
。现在,如果将
x
增加到5,则
-objectAtIndex:
将尝试从数组中获取第6个对象,该对象无法工作,因为数组仅包含5个对象-因此,该索引超出数组的范围(因为只有索引0..4是
x
的有效值)

为了避免这种情况,在实际尝试访问数组元素之前,应始终检查索引是否在数组的当前边界内:

double currentNumber = 0.;
if (x < [timeArray count]) {
    currentNumber = [[timeArray objectAtIndex:x] doubleValue];
}
double currentNumber=0。;
如果(x<[timeArray count]){
currentNumber=[[timeArray objectAtIndex:x]doubleValue];
}