【问题标题】:'NSInvalidArgumentException', reason: '*** -[NSMutableArray removeObjectsAtIndexes:]: index set cannot be nil'NSInvalidArgumentException',原因:'*** -[NSMutableArray removeObjectsAtIndexes:]:索引集不能为 nil
【发布时间】:2015-02-20 07:41:13
【问题描述】:

由于未捕获的异常“NSInvalidArgumentException”而终止应用,原因:“* -[NSMutableArray removeObjectsAtIndexes:]: index set cannot be nil”

#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

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

【问题讨论】:

    标签: ios objective-c xcode nsmutablearray


    【解决方案1】:
    [myarray removeObjectsAtIndexes:0]
    

    方法不对。使用

    [myarray removeObjectAtIndex:0]
    

    相反。

    【讨论】:

    • 为什么需要for循环?您可以直接访问该对象。
    【解决方案2】:

    如果你想从单个索引中删除对象,你应该使用:

    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]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      • 2014-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多