[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]

Sonic picture Sonic · Oct 22, 2014 · Viewed 12.8k times · Source

i'm a new iOS developer; i have an application with parse and dynamic cell but when i run the app i find that the app is crashed due to the reason on the title my code as the following

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    
    myArray = [[NSMutableArray alloc] init];
    
    
    //create new view if no view is available for recycling
    
   // view = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 250.0f, 300.0f)] autorelease];
    
    FXImageView *imageView = [[[FXImageView alloc] initWithFrame:CGRectMake(0, 0, 250.0f, 350.0f)] autorelease];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.asynchronous = YES;
   // imageView.reflectionScale = 0.5f;
   // imageView.reflectionAlpha = 0.25f;
  //  imageView.reflectionGap = 10.0f;
    imageView.shadowOffset = CGSizeMake(0.0f, 2.0f);
    imageView.shadowBlur = 5.0f;
    imageView.cornerRadius = 10.0f;
    view = imageView;
    
    [ProgressHUD dismiss];
    
     NSString *string1 = [[NSUserDefaults standardUserDefaults] stringForKey:@"Class"];
    
    PFQuery *query = [PFQuery queryWithClassName:[NSString stringWithFormat:@"%@",string1]];
    
    
    query.cachePolicy = kPFCachePolicyNetworkElseCache;
    
    //show loader view
    
    
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            
            myArray = [[NSMutableArray alloc] initWithArray:objects];
            
            PFObject *object = [myArray objectAtIndex:index];
            
            
            [file getDataInBackgroundWithBlock:^(NSData *data1, NSError *error) {
                if (!error) {
                    
                    ((UIImageView *)view).image = [UIImage imageWithData:data1];
                    
                    //[HUD hideUIBlockingIndicator];
                    
                }
            }];
            
        }
        
    }];

    return view;

    
}

i use for first screen UICollectionView with dynamic data from parse as the following code

pragma collectionView delegate

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    
    CALayer *layer = cell.layer;
    [layer setCornerRadius:8.0f];
    [layer setMasksToBounds:YES];
   // [layer setBorderWidth:1.0f];
  //  layer.backgroundColor = [UIColor whiteColor].CGColor;
    //can you click
    
    PFObject *imageObject = [myArray objectAtIndex:indexPath.item];
    PFFile *imageFile = [imageObject objectForKey:@"image"];
    NSString *name = [imageObject objectForKey:@"name"];
    
    [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
        if (!error) {
            
            UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
            imageView.image = [UIImage imageWithData:data];
            
            UILabel *title2 = (UILabel *)[cell viewWithTag:200];
            title2.text = [NSString stringWithFormat:@"%@",name];
            title2.font = [UIFont fontWithName:@"GESSTextMedium-Medium" size:12];
        }
    }];

    
    return cell;
}

i need some help to know where is the error; every time i receive this error on console [__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]

Answer

Silmaril picture Silmaril · Oct 23, 2014

There is not enough information. We even don't know the exact line for this crash. What is the actual number of objects in myArray? What you return on collectionView:numberOfItemsInSection: and so on..

Imho, the easiest solution would be debugging this crash. And finding out why index is larger then objectAtIndex: expected. Try this:

1) Open "Breakpoint navigator"

2) tap '+' button and choose "Add Exception Breakpoint" option

Now app will use this breakpoint on any exception while debugging.