IOS App crash: [__NSArrayI objectAtIndex:]: index 0 outside for an empty array

I have a method in which I pass 'id':

NSString *id = @"4bf58dd8d48988d181941735"; [self getNames:id]; 

This works fine, but when I use the "id" from the object passed within the segment:

 NSString *id = _selectedStore._id; [self getNames:id]; 

I get an error message:

 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array' 

What's happening?

Here he gets an error. My attempt (remember that I am new to this) is to take an array of Foursquare IDs, get the names, and then get photoUrl for the ID. If I delete all the code creating photoUrl (I noticed in the code), it works without crashing.

 - (void)getNames { NSMutableArray *final = [[NSMutableArray alloc] init]; for (SearchVenue *object in self.venues) { [Foursquare2 venueGetDetail:object._id callback:^(BOOL success, id result){ if (success) { NSDictionary *dic = result; NSArray *venue = [dic valueForKeyPath:@"response.venue"]; self.venueDetail = venue; NSMutableDictionary *newVen = [NSMutableDictionary dictionary]; [newVen setValue:[self.venueDetail valueForKey:@"name"] forKey:@"name"]; [final addObject:newVen]; [Foursquare2 venueGetPhotos:object._id limit:nil offset:nil callback:^(BOOL success, id result){ if (success) { NSDictionary *dic = result; NSArray *photos = [dic valueForKeyPath:@"response.photos"]; self.venuePhotos = photos; //works when removed from here... NSString *prefix = [[self.venuePhotos valueForKeyPath:@"items.prefix"] objectAtIndex:0]; NSString *size = @"100x100"; NSString *suffix = [[self.venuePhotos valueForKeyPath:@"items.suffix"] objectAtIndex:0]; NSArray *myStrings = [NSArray arrayWithObjects:prefix,size,suffix, nil]; NSString *photoUrl = [myStrings componentsJoinedByString:@"" ]; //...to here NSMutableDictionary *newVen1 = [NSMutableDictionary dictionary]; [newVen1 setValue:photoUrl forKey:@"photoUrl"]; for(int i = 0; i < [final count]; i++) { [[final objectAtIndex:i] addEntriesFromDictionary:newVen1]; } _arrayOfPlaces = final; NSLog(@"_arrayOfPlaces is %@",_arrayOfPlaces); [self.resultsVC reloadData]; } else { NSLog(@"%@",result); } }]; } else { NSLog(@"%@",result); } }]; } } 
+6
source share
2 answers

Your problem is probably here:

 NSString *prefix = [[self.venuePhotos valueForKeyPath:@"items.prefix"] objectAtIndex:0]; 

You must do this:

 if ([[self.venuePhotos valueForKeyPath:@"items.prefix"] count] > 0) { NSString *prefix = [[self.venuePhotos valueForKeyPath:@"items.prefix"] objectAtIndex:0] ... } else { // Do something for not having the prefix } 

if you want to be ultra-safe, which is always good, do it

 if ([[self.venuePhotos valueForKeyPath:@"items.prefix"] isKindOfClass:[NSArray class]] && [[self.venuePhotos valueForKeyPath:@"items.prefix"] count] > 0) 

This also ensures that the element is an array. If 1 [self.venuePhotos valueForKeyPath: @ "items.prefix"] 1 is not, and it does not respond to the account, it will fail.

A general rule, which is rarely executed sequentially, always checks the boundaries before accessing the array using an index. This is regardless of whether you get it by index or AtIndex object.

+2
source
  id object = __NSArrayI[i]; NSUInteger index = [__NSArrayI indexOfObject:object]; 

try it. first check the number of arrays.

0
source

Source: https://habr.com/ru/post/978975/


All Articles