Iphone -[\uu NSArrayM objectAtIndex:]:索引4294967295超出arc4random空数组的界限
编辑:我将arc4random()替换为arc4random_uniform()进行修复 我用试飞来监控坠机。 一直在修复bug,但是我遇到了这个bug,我不知道为什么索引这么大Iphone -[\uu NSArrayM objectAtIndex:]:索引4294967295超出arc4random空数组的界限,iphone,ios,xcode,nsmutablearray,arc4random,Iphone,Ios,Xcode,Nsmutablearray,Arc4random,编辑:我将arc4random()替换为arc4random_uniform()进行修复 我用试飞来监控坠机。 一直在修复bug,但是我遇到了这个bug,我不知道为什么索引这么大 -[__NSArrayM objectAtIndex:]: index 4294967295 beyond bounds for empty array 很可能这就是bug所在的地方 for (NSUInteger i = 0; i < count; ++i) { // Select a ra
-[__NSArrayM objectAtIndex:]: index 4294967295 beyond bounds for empty array
很可能这就是bug所在的地方
for (NSUInteger i = 0; i < count; ++i) {
// Select a random element between i and end of array to swap with.
int nElements = count - i;
int n = (arc4random() % nElements) + i;
[randomName exchangeObjectAtIndex:i withObjectAtIndex:n];
[randomImgName exchangeObjectAtIndex:i withObjectAtIndex:n];
[randomID exchangeObjectAtIndex:i withObjectAtIndex:n];
}
我相信这是错误,但我不知道如何解决错误
感谢所有的评论和帮助
-(void)defaultFrogSetup
{
self.imageArray = [[NSMutableArray alloc] initWithObjects:
[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"defaultFrog/d7.jpg"]],
[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"defaultFrog/d9.jpg"]],
[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"defaultFrog/d11.jpg"]],
[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"defaultFrog/d27.jpg"]],
[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"defaultFrog/d6.jpg"]],
nil];
//self.defaultFrogID = [[NSMutableArray alloc] initWithObjects:@"6",@"8",@"10",@"26",@"5",nil];
self.defaultFrogID = [[NSMutableArray alloc] initWithObjects:@"6",@"8",@"10",@"24",@"5",nil];
self.defaultFrogImageCaption = [[NSMutableArray alloc] initWithObjects:@"Verreaux's Tree Frog",@"Green Tree Frog",@"Red-Eyed Tree Frog",@"Crucifix Frog",@"Eastern Dwarf Tree Frog",nil];
NSUInteger count = [self.defaultFrogID count];
//NSLog(@"Count %i",[self.defaultFrogID count]);
NSMutableArray *randomImgName = self.imageArray;
NSMutableArray *randomID = self.defaultFrogID;
NSMutableArray *randomName = self.defaultFrogImageCaption;
//likely bug here
for (NSUInteger i = 0; i < count; ++i) {
// Select a random element between i and end of array to swap with.
int nElements = count - i;
int n = (arc4random() % nElements) + i;
[randomName exchangeObjectAtIndex:i withObjectAtIndex:n];
[randomImgName exchangeObjectAtIndex:i withObjectAtIndex:n];
[randomID exchangeObjectAtIndex:i withObjectAtIndex:n];
}
self.imageArray = randomImgName;
self.defaultFrogID=randomID;
self.defaultFrogImageCaption=randomName;
//NSLog(@"default filename %@",self.defaultFrogImageCaption);
//NSLog(@"default frog ID %@",self.defaultFrogID);
self.imageViewTop.alpha = 1.0;
self.imageViewBottom.alpha = 0.0;
self.imageViewBottom = [[UIImageView alloc] initWithFrame:CGRectMake(0,44,320,367)];
self.imageViewTop = [[UIImageView alloc] initWithFrame:CGRectMake(0,44,320,367)];
[self.view addSubview:imageViewTop];
[self.view addSubview:imageViewBottom];
self.buttonCaption = [UIButton buttonWithType:UIButtonTypeCustom];
//default placement
self.buttonCaption.frame = CGRectMake(245, 367, 70, 44);
//[buttonCaption setTitle:@"\u00A9" forState:UIControlStateNormal];
[self.buttonCaption addTarget:self action:@selector(buttonCheck) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.buttonCaption];
[self defaultFrogAnimation:buttonCaption.frame.size.width];
}
-(void)defaultFrogAnimation:(float)previousWidth {
defaultFrog = 1;
foundFrog = 0;
//picture loop
imageViewTop.image = imageViewBottom.image;
imageViewBottom.image = [imageArray objectAtIndex:[imageArray count] - 1];
[imageArray insertObject:imageViewBottom.image atIndex:0];
[imageArray removeLastObject];
imageViewTop.alpha = 1.0;
imageViewBottom.alpha = 0.0;
//Frog Name Caption
NSString * tempCaption = [defaultFrogImageCaption objectAtIndex:[defaultFrogImageCaption count]-1];
self.dFrogID = [defaultFrogID objectAtIndex:[defaultFrogID count]-1];
// make the buttons content appear in the top-left
[buttonCaption setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[buttonCaption setContentVerticalAlignment: UIControlContentVerticalAlignmentCenter ];
[defaultFrogImageCaption insertObject:tempCaption atIndex:0];
[defaultFrogImageCaption removeLastObject];
[defaultFrogID insertObject:dFrogID atIndex:0];
[defaultFrogID removeLastObject];
//button setting
[buttonCaption.titleLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[buttonCaption.titleLabel setFont:[UIFont systemFontOfSize:17]];
[buttonCaption setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[[buttonCaption layer] setCornerRadius:5.0f];
[[buttonCaption layer] setMasksToBounds:YES];
[[buttonCaption layer] setBackgroundColor:[[UIColor colorWithRed:255 green:255 blue:255 alpha:1] CGColor]];
[buttonCaption.titleLabel setFrame:CGRectMake(0,9, 25, 25)];
stringsize = [tempCaption sizeWithFont:[UIFont systemFontOfSize:19]];
CGFloat diff = stringsize.width - previousWidth;
//NSLog(@"diff %f",diff);
[UIView animateWithDuration:3
animations:^{
imageViewTop.alpha = 0.0;
imageViewBottom.alpha = 1.0;
}
completion:^(BOOL completed){
if (completed)
[self defaultFrogAnimation:stringsize.width];
}
];
[UIView animateWithDuration:0.1 delay:0 options:0
animations:^{
[buttonCaption setTitle:tempCaption forState:UIControlStateNormal];
NSLog(@"frog Name %@",tempCaption );
NSLog(@"frog ID %@",dFrogID);
}
completion:^(BOOL completed)
{
}];
[UIView animateWithDuration:0.3 delay:0 options:0
animations:^{
[buttonCaption setFrame:CGRectMake(buttonCaption.frame.origin.x-diff,
buttonCaption.frame.origin.y,
buttonCaption.frame.size.width + diff,
buttonCaption.frame.size.height)];
}
completion:^(BOOL completed){
}];
}
-(void)defaultFrogSetup
{
self.imageArray=[[NSMutableArray alloc]initWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@“defaultFrog/d7.jpg”],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@“defaultFrog/d9.jpg”],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@“defaultFrog/d11.jpg”],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@“defaultFrog/d27.jpg”],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@“defaultFrog/d6.jpg”],
零];
//self.defaultFrogID=[[NSMutableArray alloc]initWithObjects:@“6”、“8”、“10”、“26”、“5”、“nil];
self.defaultFrogID=[[NSMutableArray alloc]initWithObjects:@“6”、“8”、“10”、“24”、“5”、“nil];
self.defaultFrogImageCaption=[[NSMutableArray alloc]initWithObjects:@“Verreaux的树蛙”,“绿色树蛙”,“红眼树蛙”,“十字架蛙”,“东方矮树蛙”,无];
NSU整数计数=[self.defaultFrogID计数];
//NSLog(@“计数%i,[self.defaultFrogID计数]);
NSMutableArray*随机imgname=self.imageArray;
NSMutableArray*randomID=self.defaultFrogID;
NSMutableArray*randomName=self.defaultFrogImageCaption;
//这里可能有虫子
对于(整数i=0;i-(void)defaultFrogSetup
{
self.imageArray = [[NSMutableArray alloc] initWithObjects:
[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"defaultFrog/d7.jpg"]],
[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"defaultFrog/d9.jpg"]],
[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"defaultFrog/d11.jpg"]],
[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"defaultFrog/d27.jpg"]],
[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"defaultFrog/d6.jpg"]],
nil];
//self.defaultFrogID = [[NSMutableArray alloc] initWithObjects:@"6",@"8",@"10",@"26",@"5",nil];
self.defaultFrogID = [[NSMutableArray alloc] initWithObjects:@"6",@"8",@"10",@"24",@"5",nil];
self.defaultFrogImageCaption = [[NSMutableArray alloc] initWithObjects:@"Verreaux's Tree Frog",@"Green Tree Frog",@"Red-Eyed Tree Frog",@"Crucifix Frog",@"Eastern Dwarf Tree Frog",nil];
NSUInteger count = [self.defaultFrogID count];
//NSLog(@"Count %i",[self.defaultFrogID count]);
NSMutableArray *randomImgName = self.imageArray;
NSMutableArray *randomID = self.defaultFrogID;
NSMutableArray *randomName = self.defaultFrogImageCaption;
//likely bug here
for (NSUInteger i = 0; i < count; ++i) {
// Select a random element between i and end of array to swap with.
int nElements = count - i;
int n = (arc4random() % nElements) + i;
[randomName exchangeObjectAtIndex:i withObjectAtIndex:n];
[randomImgName exchangeObjectAtIndex:i withObjectAtIndex:n];
[randomID exchangeObjectAtIndex:i withObjectAtIndex:n];
}
self.imageArray = randomImgName;
self.defaultFrogID=randomID;
self.defaultFrogImageCaption=randomName;
//NSLog(@"default filename %@",self.defaultFrogImageCaption);
//NSLog(@"default frog ID %@",self.defaultFrogID);
self.imageViewTop.alpha = 1.0;
self.imageViewBottom.alpha = 0.0;
self.imageViewBottom = [[UIImageView alloc] initWithFrame:CGRectMake(0,44,320,367)];
self.imageViewTop = [[UIImageView alloc] initWithFrame:CGRectMake(0,44,320,367)];
[self.view addSubview:imageViewTop];
[self.view addSubview:imageViewBottom];
self.buttonCaption = [UIButton buttonWithType:UIButtonTypeCustom];
//default placement
self.buttonCaption.frame = CGRectMake(245, 367, 70, 44);
//[buttonCaption setTitle:@"\u00A9" forState:UIControlStateNormal];
[self.buttonCaption addTarget:self action:@selector(buttonCheck) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.buttonCaption];
[self defaultFrogAnimation:buttonCaption.frame.size.width];
}
-(void)defaultFrogAnimation:(float)previousWidth {
defaultFrog = 1;
foundFrog = 0;
//picture loop
imageViewTop.image = imageViewBottom.image;
imageViewBottom.image = [imageArray objectAtIndex:[imageArray count] - 1];
[imageArray insertObject:imageViewBottom.image atIndex:0];
[imageArray removeLastObject];
imageViewTop.alpha = 1.0;
imageViewBottom.alpha = 0.0;
//Frog Name Caption
NSString * tempCaption = [defaultFrogImageCaption objectAtIndex:[defaultFrogImageCaption count]-1];
self.dFrogID = [defaultFrogID objectAtIndex:[defaultFrogID count]-1];
// make the buttons content appear in the top-left
[buttonCaption setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[buttonCaption setContentVerticalAlignment: UIControlContentVerticalAlignmentCenter ];
[defaultFrogImageCaption insertObject:tempCaption atIndex:0];
[defaultFrogImageCaption removeLastObject];
[defaultFrogID insertObject:dFrogID atIndex:0];
[defaultFrogID removeLastObject];
//button setting
[buttonCaption.titleLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[buttonCaption.titleLabel setFont:[UIFont systemFontOfSize:17]];
[buttonCaption setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[[buttonCaption layer] setCornerRadius:5.0f];
[[buttonCaption layer] setMasksToBounds:YES];
[[buttonCaption layer] setBackgroundColor:[[UIColor colorWithRed:255 green:255 blue:255 alpha:1] CGColor]];
[buttonCaption.titleLabel setFrame:CGRectMake(0,9, 25, 25)];
stringsize = [tempCaption sizeWithFont:[UIFont systemFontOfSize:19]];
CGFloat diff = stringsize.width - previousWidth;
//NSLog(@"diff %f",diff);
[UIView animateWithDuration:3
animations:^{
imageViewTop.alpha = 0.0;
imageViewBottom.alpha = 1.0;
}
completion:^(BOOL completed){
if (completed)
[self defaultFrogAnimation:stringsize.width];
}
];
[UIView animateWithDuration:0.1 delay:0 options:0
animations:^{
[buttonCaption setTitle:tempCaption forState:UIControlStateNormal];
NSLog(@"frog Name %@",tempCaption );
NSLog(@"frog ID %@",dFrogID);
}
completion:^(BOOL completed)
{
}];
[UIView animateWithDuration:0.3 delay:0 options:0
animations:^{
[buttonCaption setFrame:CGRectMake(buttonCaption.frame.origin.x-diff,
buttonCaption.frame.origin.y,
buttonCaption.frame.size.width + diff,
buttonCaption.frame.size.height)];
}
completion:^(BOOL completed){
}];
}