nsarray - IOS App crash: [__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array -


i have method pass 'id' here:

nsstring *id = @"4bf58dd8d48988d181941735"; [self getnames:id]; 

this works fine, when use 'id' object passed within segue:

nsstring *id = _selectedstore._id; [self getnames:id]; 

i error:

'nsrangeexception', reason: '*** -[__nsarrayi objectatindex:]: index 0 beyond bounds empty array' 

what's going on?

here's it's getting error. attempt (keep in mind i'm new @ this) take array of foursquare id's, names , photourl id's. if remove code creates photourl (i've noted in code) runs without crashing.

- (void)getnames {     nsmutablearray *final = [[nsmutablearray alloc] init];     (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 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 = 0; < [final count]; i++) {                                [[final objectatindex:i] addentriesfromdictionary:newven1];                             }                             _arrayofplaces = final;                             nslog(@"_arrayofplaces %@",_arrayofplaces);                             [self.resultsvc reloaddata];                         } else {                             nslog(@"%@",result);                         }                     }];             } else {                 nslog(@"%@",result);               }         }];     } } 

your problem here:

nsstring *prefix =   [[self.venuephotos valueforkeypath:@"items.prefix"] objectatindex:0]; 

you should this:

if ([[self.venuephotos valueforkeypath:@"items.prefix"] count] > 0) { nsstring *prefix =   [[self.venuephotos valueforkeypath:@"items.prefix"] objectatindex:0] ... } else { // not having prefix } 

if want ultra-safe, good, this

if ([[self.venuephotos valueforkeypath:@"items.prefix"]                           iskindofclass:[nsarray class]]   && [[self.venuephotos valueforkeypath:@"items.prefix"] count] > 0) 

this ensure item array. if 1[self.venuephotos valueforkeypath:@"items.prefix"]1 isn't , doesn't respond count, crash.

generalized rule of thumb, seldom consistently followed check bounds prior accessing array using index. regardless of whether subscript or objectatindex.


Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -