数组值减少

时间:2012-11-28 10:04:48

标签: iphone ios xcode nsmutablearray

我使用以下代码选择正确的答案,当它达到限制时,它会显示结果视图,

-(void)displayImageAndButton:(int)pos
{
CATransition *transition = [CATransition animation];
transition.duration = 1.0f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;

[animage.layer addAnimation:transition forKey:nil];
animage.image=[UIImage  imageNamed:[NSString stringWithFormat:@"%@.jpg",[imageary objectAtIndex:pos]]];
//NSLog(@"pos value%d",pos);
UIView *newView = [self.view viewWithTag:1];
if([newView isKindOfClass:[UIButton class]])
{
    NSLog(@"pos value%d",pos);
    UIButton *newButton = (UIButton *)newView;
    name=[imageary objectAtIndex:pos++];
    [newButton addTarget:self action:@selector(submitGuess:)
        forControlEvents:UIControlEventValueChanged];
    [newButton setTitle:name forState:UIControlStateNormal];
}
newView = [self.view viewWithTag:2];
if([newView isKindOfClass:[UIButton class]])
{
    NSLog(@"pos value%d",pos);
    UIButton *newButton = (UIButton *)newView;
    name=[imageary objectAtIndex:pos++];
    [newButton addTarget:self action:@selector(submitGuess:)
        forControlEvents:UIControlEventValueChanged];
    [newButton setTitle:name forState:UIControlStateNormal];
}
newView = [self.view viewWithTag:3];
if([newView isKindOfClass:[UIButton class]])
{
    NSLog(@"pos value%d",pos);
    UIButton *newButton = (UIButton *)newView;
    name=[imageary objectAtIndex:pos++];
    [newButton addTarget:self action:@selector(submitGuess:)
        forControlEvents:UIControlEventValueChanged];
    [newButton setTitle:name forState:UIControlStateNormal];
}
newView = [self.view viewWithTag:4];
if([newView isKindOfClass:[UIButton class]])
{
    NSLog(@"pos value%d",pos);
    UIButton *newButton = (UIButton *)newView;
    name=[imageary objectAtIndex:pos++];// Here the problem coming when it value 13 its getting error what should i change
    [newButton addTarget:self action:@selector(submitGuess:)
        forControlEvents:UIControlEventValueChanged];
    [newButton setTitle:name forState:UIControlStateNormal];
}

}

更新^^^

- (IBAction)submitGuess:(id)sender
{
    UIButton *mybutton = (UIButton *)sender;

    if ([mybutton.titleLabel.text isEqualToString:[imageary objectAtIndex:positionOfQuest]])
    {
        //titles equal
        [self alertshow];
        positionOfQuest++;
        totalGuesses++;
        timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(removeImageView) userInfo:nil repeats:NO];
        [self displayImageAndButton:positionOfQuest];
        [lb1 setText:[NSString stringWithFormat:@"Question %i ", positionOfQuest + 1]];
    } else {
        totalGuesses++;
        [self alertshow2];
    }

    NSLog(@"toatguess value%d",positionOfQuest);
    NSLog(@"toatguess value%d",numCorrect);
    if(positionOfQuest==numCorrect)
    {
        [self resulmsg];
        [lb3 setText:[NSString stringWithFormat: @"%i guesses, %.02f%% correct", totalGuesses,
                      1000 / (float)totalGuesses]];

        [lb4 setText:[NSString stringWithFormat: @"%.02f%%",1000 / (float)totalGuesses]];
        NSLog(@"lb4%@",lb4);
    }
}

我的限制为10,即。 numcorrect值为10.但是当它达到9时,它显示错误。

2012-11-28 15:27:56.430 Kidsapp[4225:c07] toatguess value8
2012-11-28 15:27:56.431 Kidsapp[4225:c07] toatguess value10
2012-11-28 15:27:57.409 Kidsapp[4225:c07] toatguess value9
2012-11-28 15:27:57.409 Kidsapp[4225:c07] toatguess value10
2012-11-28 15:27:58.017 Kidsapp[4225:c07] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 13 beyond bounds [0 .. 12]'

这里有什么错误?

3 个答案:

答案 0 :(得分:1)

好吧,至于你的错误,它清楚你正在尝试访问12数组的第13个对象。尝试设置一些断点来调试你的代码。

答案 1 :(得分:1)

数组以索引0开头,因此您的最大索引值必须为[[array count] -1]。 根据你的代码positionOfQuest获得值13你得到错误

因为您的数组计数为13但是数组的索引从12开始为0.

答案 2 :(得分:0)

这是因为您的positionOfQuest获得了值13,而您正在搜索

[imageary objectAtIndex:12];  

但是图像有对象直到索引12(总共13个对象索引0 ... 12)

所以根据你的问题,只需制作一个if条件,如

if ([imageary count] > positionOfQuest)
{
    if ([mybutton.titleLabel.text isEqualToString:[imageary objectAtIndex:positionOfQuest]])
    {
        //Your work
    }

}