title: NSRangeException NSArray objectAtIndex: index 11 beyond bounds for empty array Objective C
body: <p>I have a search bar that searches the 4square Api for venues.</p>
  
  <p>Sometimes when I search for a specific word and scroll down my app crashes with a empty array.</p>
  
  <p>Below is my code and error code.</p>
  
  <pre><code>*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 11 beyond bounds for empty array'
  
  *** First throw call stack:
  (0x335683e7 0x3b259963 0x334b321d 0xc7ce3 0x353c2569 0x353a7391 0x353be827 0x3537a8c7 0x35126513 0x351260b5 0x35126fd9 0x351269c3 0x351267d5 0x35126639 0x3353d941 0x3353bc39 0x334af263 0x334af0c9 0x3708d33b 0x353cb2b9 0xc173d 0x3b686b20)
  libc++abi.dylib: terminate called throwing an exception
  (lldb) 
  
  libsystem_kernel.dylib`__pthread_kill:
  0x3b74d348:  mov    r12, #328
  0x3b74d34c:  svc    #128
  0x3b74d350:  blo    0x3b74d368                ; __pthread_kill + 32
  0x3b74d354:  ldr    r12, [pc, #4]             ; __pthread_kill + 24
  0x3b74d358:  ldr    r12, [pc, r12]
  0x3b74d35c:  b      0x3b74d364                ; __pthread_kill + 28
  0x3b74d360:  .long  0x01ac5cc4                ; unknown opcode
  0x3b74d364:  bx     r12
  0x3b74d368:  bx     lr
  </code></pre>
  
  <p>Here is my code</p>
  
  <p><strong>RecipeDetailViewController.h</strong></p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  
  @interface RecipeDetailViewController : UIViewController &lt;UITextFieldDelegate, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource, UISearchDisplayDelegate&gt;{
  UITableView *searchTableView;
  UISearchBar *sBar;
  UISearchDisplayController *searchDisplayController;
  }
  
  @property (strong, nonatomic) NSArray *loadedSearches;
  
  @end
  </code></pre>
  
  <p><strong>RecipeDetailViewController.m</strong></p>
  
  <pre><code>#import "RecipeDetailViewController.h"
  #import "AFJSONRequestOperation.h"
  
  @interface RecipeDetailViewController ()
  
  @end
  
  @implementation RecipeDetailViewController
  
  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  {
  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  if (self) {
      self.title = @"Search";
  }
  return self;
  }
  
  - (void)viewDidLoad
  {
  [super viewDidLoad];
  
  searchTableView = [[UITableView alloc] initWithFrame:self.view.bounds];
  searchTableView.delegate = self;
  searchTableView.dataSource = self;
  [self.view addSubview:searchTableView];
  
  sBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 160, 44)];
  sBar.placeholder = @"Search for Inspection Equipment...";
  sBar.tintColor = [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1.0];
  sBar.backgroundImage = [UIImage imageNamed:@"searchbarbackground.png"];
  sBar.delegate = self;
  searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:sBar contentsController:self];
  
  searchDisplayController.delegate = self;
  searchDisplayController.searchResultsDataSource = searchTableView.dataSource;
  searchDisplayController.searchResultsDelegate = searchTableView.delegate;
  
  searchTableView.tableHeaderView = sBar;
  
  //  UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
  //   [refreshControl addTarget:self action:@selector(refreshView:) forControlEvents:UIControlEventValueChanged];
  //  [searchTableView addSubview:refreshControl];
  
  }
  
  -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  {
  NSString *searchQuery = [NSString stringWithFormat:@"https://api.foursquare.com/v2/venues/search?ll=40.4263,-86.9177&amp;client_id=EI10B1E0VF2KGLXL4G5YXQVSNGXD4ABXUHOYN3RWKY3ZUPD0&amp;client_secret=D4BT4LC3MTNULCGLC30YKWITCHIOURNOEXRR04AL3H4YUVKK&amp;v=20121223&amp;query='%@'",searchText];
  
  searchQuery = [searchQuery stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  
  NSURL *url = [[NSURL alloc] initWithString:searchQuery];
  
  NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
  
  AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                                                      success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
                                       {
                                           self.loadedSearches = JSON[@"response"][@"venues"];
  
                                           // refreshing the TableView when the block gets the response
                                           [searchTableView reloadData];
                                       }
                                                                                      failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
                                       {
                                           NSLog(@"%@", error.localizedDescription);
                                       }];
  
  [operation start];
  
  
  }
  
  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
  return 1;
  }
  
  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
  return self.loadedSearches.count;
  }
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
  
  if(cell == nil) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
  }
  
  cell.textLabel.text = self.loadedSearches[indexPath.row][@"name"];
  
  return cell;
  }
  
  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"TableCell Tapped" message:@"Yeah you tapped a table cell" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  [alert show];
  [tableView deselectRowAtIndexPath:indexPath animated:YES];
  
  }
  </code></pre>