2014-12-02 14 views
6

ho un metodo che passare un 'id' in questa sede:incidente IOS App: [__NSArrayI objectAtIndex:]: indice 0 dismisura per array vuoto

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

Questo funziona bene, ma quando uso il ' id' da un oggetto superato entro la segue:

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

ottengo l'errore:

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

Cosa sta succedendo?

Ecco dove sta ottenendo l'errore. Il mio tentativo (tieni a mente che sono nuovo a questo) è di prendere una serie di ID di Foursquare, ottenere i nomi e poi ottenere la fotoUrl per gli id. Se rimuovo tutto il codice che crea la fotoUrl (che ho notato nel codice) funziona senza crash.

- (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); 
       } 
     }]; 
    } 
} 
+0

controllare il conteggio degli array, prima di controllare l'oggetto all'indice. – Savitha

+0

"id" è una parola chiave in-build per l'obiettivo-C. puoi per favore provare cambiando il nome della stringa in valId o in qualsiasi altra cosa. – Esha

+0

l'ho modificato in 'identificazione' ma si verifica ancora un errore. – Ryasoh

risposta

2

Il tuo problema è probabilmente qui:

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

Si dovrebbe fare questo:

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 
} 

se si vuole essere ultra-sicuro, che è sempre bene, farlo

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

Ciò garantirà inoltre che l'elemento sia un array. Se 1 [self.venuePhotos valueForKeyPath: @ "items.prefix"] 1 non lo è e non risponde al conteggio, si bloccherà.

La regola generale generalizzata, che viene raramente seguita in modo coerente, controlla sempre i limiti prima di accedere a un array utilizzando un indice. Questo indipendentemente dal fatto che tu lo ottenga per subscript o objectAtIndex.

0
id object = __NSArrayI[i]; 

NSUInteger index = [__NSArrayI indexOfObject:object]; 

provare this..first controllare il valore del tuo array.

+2

Questo codice non è corretto. –