' NSInvalidArgumentException',原因:' *** - [NSMutableArray removeObjectsAtIndexes:]:索引集不能为零

时间:2015-02-20 07:41:13

标签: ios objective-c xcode nsmutablearray

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' * - [NSMutableArray removeObjectsAtIndexes:]:索引集不能为零

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

CGPoint touchPointbegin;
CGPoint touchPointend;
CGPoint difference;
CGPoint Velocity;
bool touchEnd;
bool touchBegin;
int i,j;
int count =0;

- (void)viewDidLoad {
    [super viewDidLoad];

    myarray=[[NSMutableArray alloc] init];
    j=10;

    for (i=0; i<5; i++)
    {
        UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(142, j, 35, 35)];
        img.image=[UIImage imageNamed:@"1.png"];
        [self.view addSubview:img];
        img.userInteractionEnabled=true;
        [myarray addObject:img];
        j=j+45;
    }
    Timer=[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(Update_loop) userInfo:nil repeats:YES];
    touchEnd=false;
    touchBegin=false;

}


-(void) Update_loop
{
    if(touchEnd)
    {
        for(UIImageView *image in myarray)
        {
            if(image==[myarray objectAtIndex:0])
            {
              image.center=CGPointMake((image.center.x+Velocity.x),(image.center.y+Velocity.y));
            }

            if(image.center.x<0|| image.center.x>320 || image.center.y>480|| image.center.y<0)
            {
                touchEnd=false;
                touchBegin=false;
                [image removeFromSuperview];
            }
        }

        if(!touchEnd && !touchBegin)
        {
          [myarray removeObjectsAtIndexes:0]
        }
    }
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {


    UITouch *touch = [[event allTouches] anyObject];
    if ([touch view] == [myarray objectAtIndex:0])
    {
        touchPointbegin = [[touches anyObject] locationInView:self.view];
        touchBegin=true;
    }
}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(!touchEnd && touchBegin)
    {
        touchPointend = [[touches anyObject] locationInView:self.view];
        difference.x=touchPointend.x-touchPointbegin.x;
        difference.y=touchPointend.y-touchPointbegin.y;
        Velocity.x=difference.x*0.04;
        Velocity.y=difference.y*0.04;
        touchEnd=true;
    }
    if(Velocity.x==0 && Velocity.y==0)
    {
        touchEnd=false;
    }
}


-(BOOL) prefersStatusBarHidden
{
    return YES;
}

@end

**有关如何纠正错误的任何建议。 有没有人遇到过这样的例外。需要帮助!!!!!!!! 提前谢谢。

2 个答案:

答案 0 :(得分:0)

[myarray removeObjectsAtIndexes:0]

是错误的方法。使用

[myarray removeObjectAtIndex:0]

代替。

答案 1 :(得分:0)

如果要从单个索引中删除对象,则应使用:

if(!touchEnd && !touchBegin)
{
  [myarray removeObjectAtIndex:0]
}

或者如果要删除多个索引处的对象,请创建一个NSIndexSet,然后使用以下方式使用的方法:

NSMutableIndexSet *mutableIndexSet = [[NSMutableIndexSet alloc] init];
[mutableIndexSet addIndex:0];
[mutableIndexSet addIndex:2];
[mutableIndexSet addIndex:9];

if(!touchEnd && !touchBegin)
{
  [myarray removeObjectsAtIndexes:mutableIndexSet]
}