2014-12-02 6 views
6

ich eine Methode haben passiere ich eine 'id' in hier:IOS App Absturz: [__NSArrayI objectAtIndex:]: Index 0 über Grenzen für leere Array

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

Dies funktioniert gut, aber wenn ich die ' id‘von einem Objekt innerhalb des segue bestanden:

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

ich den Fehler:

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

Was ist los?

Hier ist, wo es den Fehler erhält. Mein Versuch (denke daran, dass ich neu bin) ist, eine Reihe von Foursquare-IDs zu nehmen, die Namen zu erhalten und dann die PhotoUrl für die IDs zu holen. Wenn ich den gesamten Code lösche, der die photoUrl erstellt (ich habe es im Code notiert), läuft es ohne Absturz.

- (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

Überprüfen Sie die Array-Anzahl, bevor Sie das Objekt am Index überprüfen. – Savitha

+0

"ID" ist ein In-Build-Schlüsselwort für das Ziel-C. können Sie bitte versuchen, indem Sie Ihren String-Namen in valId oder anythings anderen ändern. – Esha

+0

Ich habe es in "Identifikation" geändert, aber immer noch tritt ein Fehler auf. – Ryasoh

Antwort

2

Ihr Problem ist wahrscheinlich hier:

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 
} 

, wenn Sie diesen

if ([[self.venuePhotos valueForKeyPath:@"items.prefix"] 
         isKindOfClass:[NSArray class]] 
&& [[self.venuePhotos valueForKeyPath:@"items.prefix"] count] > 0) 
ultra-sicher sein wollen, was immer gut ist, tun:

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

Sie dies tun sollten

Dies stellt auch sicher, dass das Objekt ein Array ist. Wenn 1 [self.venuePhotos valueForKeyPath: @ "items.prefix"] 1 nicht ist und nicht auf die Anzahl reagiert, stürzt es ab.

Die verallgemeinerte Faustregel, die selten konsequent befolgt wird, ist immer check-bounds vor dem Zugriff auf ein Array mit einem Index. Dies ist unabhängig davon, ob Sie es per Index oder objectAtIndex erhalten.

0
id object = __NSArrayI[i]; 

NSUInteger index = [__NSArrayI indexOfObject:object]; 

versuchen Sie Ihre Array Zählung this..first zu überprüfen.

+2

Dieser Code ist kein richtiger Code. –

Verwandte Themen