【发布时间】: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