0

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]

Community
  • 1
  • 1
Sonic
  • 518
  • 1
  • 7
  • 19

6 Answers6

7

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.

Silmaril
  • 4,241
  • 20
  • 22
  • i make that already and i receive the break point on that line `PFObject *object = [myArray objectAtIndex:index];` – Sonic Oct 22 '14 at 22:09
  • your `carousel:viewForItemAtIndex:reusingView:` method receives index which you are using inside `findObjectsInBackgroundWithBlock` which is wrong. At this point your array should be already available. You can't be sure if `findObjectsInBackgroundWithBlock` will find enough items – Silmaril Oct 22 '14 at 22:15
  • Work for me! Thanks!!:] – Jerome Oct 12 '16 at 02:25
2

Salutations and welcomes, new developer. The first thing you want to find out is where that exception is happening. To do that, you're going to need to turn on the "exception breakpoint".

The exact way you do that varies by Xcode version (thanks Apple). Assuming that you are using Xcode 6, there's a video here that explains it.

I think it is quite likely that your carousel is miss-reporting the number of items it has. Or for some other reason you are trying to get a carousel item that does not exist… perhaps the carousel expects a nil when it queries with an index of a cell outside of the existing range, but you're assuming the index is valid and hitting an array bounds exception. At a wild guess, possibly the error is with the lines here:

…
myArray = [[NSMutableArray alloc] initWithArray:objects];
PFObject *object = [myArray objectAtIndex:index];
…

I would guess that section is not a valid index in to myArray.

(By the way – myArray is a mutable copy of the objects array. Why are you doing this? You don't make any changes to myArray, so it seems unnecessary.)


You probably have a method collectionView: numberOfItemsInSection: in your collection view delegate. Or if you don't, you probably need one. What's the code in that at the moment?

Benjohn
  • 13,228
  • 9
  • 65
  • 127
  • i make that to join the array with data coming fro parse; also i use breakpoint and it's mentioned that the error on this line `PFObject *object = [myArray objectAtIndex:index];` – Sonic Oct 22 '14 at 22:04
  • I'll take your word for it. You have a problem here. The number of carousel items you have depends on the result of your background query `findObjectsInBackgroundWithBlock:`. There must be some mechanism for your delegate / data source code here to explain that to the carousel, but you don't have this currently. – Benjohn Oct 22 '14 at 22:12
  • `- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return [myArray count]; } ` – Sonic Oct 22 '14 at 22:21
0

Your problem is in this line: PFObject *object = [myArray objectAtIndex:index];

Your index for this is 2 but you only have 2 objects in your array. Since the max index of your array is 1 (0..1), it crashes. I suggest seeing where that method is called and how you are getting an index that is out of range.

AdamPro13
  • 7,232
  • 2
  • 30
  • 28
  • yes; you are fully right because when i use the breakpoint it's marking this line and i use po method and when i put index equal to 2 it's read null; but i don't know how i can solve the problem; when i put index equal to 1 it's working perfect but i got the same item on caresoul – Sonic Oct 22 '14 at 21:57
0

It is probably one of these two lines:

PFObject *object = [myArray objectAtIndex:index];

or

PFObject *imageObject = [myArray objectAtIndex:indexPath.item];

The problem is that index (or indexPath.item in the second case) contains the value 2, yet myArray is of size 2 (therefore the only valid indexes are 0 and 1).

Rob
  • 1,025
  • 2
  • 10
  • 27
  • no; the problem on the first line coz when i use breakpoint it marks on that line but i don't know how to solve – Sonic Oct 22 '14 at 21:58
  • which first line do you mean? The first one I mention or the first one of one of your functions? – Rob Oct 22 '14 at 22:03
  • `PFObject *object = [myArray objectAtIndex:index];` – Sonic Oct 22 '14 at 22:07
  • OK. So now add this line of code `NSLog(@"%i %i", index, myArray.count);` right above. You'll see that you're attempting to access index 2 but that the count of the array is 2. Your problem is that you should only be calling this function when index < myArray.count – Rob Oct 23 '14 at 17:20
0

finally i find the problem where; i must define the array for iCarousel numberOfItems so the code will be as the following

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    //return the total number of items in the carousel

    return ([myArray count]);
    return ([titleArray count]);
    return ([priceArray count]);
    return ([sapArray count]);    
}

so i escape from the index problem

thanks for all sharing with me

Rémi Santos
  • 143
  • 1
  • 9
Sonic
  • 518
  • 1
  • 7
  • 19
0

Please check your numberOfRowsInSection method and array count you are using.

Narasimha Nallamsetty
  • 1,215
  • 14
  • 16