2014-12-02 50 views
6

我有一個方法,我傳遞一個「身份證」到這裏:iOS應用崩潰:[__NSArrayI objectAtIndex:]:索引0超越界限的空數組

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

這工作得很好,但是當我使用'從對象ID」通過SEGUE內:

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

我得到的錯誤:

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

這是怎麼回事?

下面是它出現錯誤的地方。我的嘗試(請記住我是新手)是取一個Foursquare ID的數組,獲取名稱,然後獲取id的photoUrl。如果我刪除所有創建photoUrl的代碼(我已經在代碼中註明),它會運行而不會崩潰。

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

在檢查索引處的對象之前檢查數組數量。 – Savitha 2014-12-02 06:20:05

+0

「id」是Objective-C的內部生成關鍵字。你可以請嘗試改變你的字符串名稱valId或其他任何東西。 – Esha 2014-12-02 06:21:29

+0

我將它改爲'識別',但仍然出現錯誤。 – Ryasoh 2014-12-02 06:23:27

回答

2

你的問題很可能是在這裏:

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

你應該這樣做:

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 ([[self.venuePhotos valueForKeyPath:@"items.prefix"] 
         isKindOfClass:[NSArray class]] 
&& [[self.venuePhotos valueForKeyPath:@"items.prefix"] count] > 0) 

這也將確保該項目是一個數組。如果1 [self.venuePhotos valueForKeyPath:@「items.prefix」] 1不是,它不響應計數,它會崩潰。

通常的經驗法則,在使用索引訪問數組之前總是檢查邊界。這與您是通過下標還是通過objectAtIndex獲取它無關。

0
id object = __NSArrayI[i]; 

NSUInteger index = [__NSArrayI indexOfObject:object]; 

嘗試this..first檢查數組數。

+2

該代碼不是正確的代碼。 – 2014-12-02 06:57:47

相關問題