title: iOS: Sometimes app is terminated
body: <p>My developing iOS application is <strong>sometimes</strong> terminated after the method <code>addSubview:</code> is called in the application delegate (but I'm not sure <code>removeFromSuperview:</code> also involved):</p>
  
  <pre><code>[self.tabBarController.view removeFromSuperview];    
  [self.window addSubview:navigationController.view];
  </code></pre>
  
  <p>What that showed in the log is:</p>
  
  <pre><code>*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
  *** Call stack at first throw:
  (
      0   CoreFoundation                      0x019b75a9 __exceptionPreprocess + 185
      1   libobjc.A.dylib                     0x01c41313 objc_exception_throw + 44
      2   CoreFoundation                      0x019ad0a5 -[__NSArrayM objectAtIndex:] + 261
      3   &lt;app_name&gt;                          0x000186dd -[TableViewController tableView:numberOfRowsInSection:] + 205
      4   UIKit                               0x00a742b7 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 1834
      5   UIKit                               0x00a71eb1 -[UITableViewRowData numberOfRowsInSection:] + 110
      6   UIKit                               0x00b60807 -[UISearchDisplayController _updateNoSearchResultsMessageVisiblity] + 115
      7   Foundation                          0x01029669 _nsnote_callback + 145
      8   CoreFoundation                      0x0198f9f9 __CFXNotificationPost_old + 745
      9   CoreFoundation                      0x0190e93a _CFXNotificationPostNotification + 186
      10  Foundation                          0x0101f20e -[NSNotificationCenter postNotificationName:object:userInfo:] + 134
      11  UIKit                               0x008df649 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 4581
      12  UIKit                               0x008d9254 -[UIWindow _setRotatableViewOrientation:duration:force:] + 89
      13  UIKit                               0x008db38f -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 794
      14  UIKit                               0x008d9081 -[UIWindow setDelegate:] + 252
      15  UIKit                               0x00964dee -[UIViewController _tryBecomeRootViewControllerInWindow:] + 106
      16  UIKit                               0x00975340 -[UINavigationController viewDidMoveToWindow:shouldAppearOrDisappear:] + 72
      17  UIKit                               0x008f0e4b -[UIView(Internal) _didMoveFromWindow:toWindow:] + 918
      18  UIKit                               0x008efa60 -[UIView(Hierarchy) _postMovedFromSuperview:] + 166
      19  UIKit                               0x008e8750 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1080
      20  UIKit                               0x008e6aa3 -[UIView(Hierarchy) addSubview:] + 57
      21  &lt;app_name&gt;                          0x00004971 -[AppDelegate requestFinished:] + 993
      22  &lt;app_name&gt;                          0x0003e65b -[ASIHTTPRequest reportFinished] + 171
      23  Foundation                          0x0103e94e __NSThreadPerformPerform + 251
      24  CoreFoundation                      0x019988ff __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
      25  CoreFoundation                      0x018f688b __CFRunLoopDoSources0 + 571
      26  CoreFoundation                      0x018f5d86 __CFRunLoopRun + 470
      27  CoreFoundation                      0x018f5840 CFRunLoopRunSpecific + 208
      28  CoreFoundation                      0x018f5761 CFRunLoopRunInMode + 97
      29  GraphicsServices                    0x01db11c4 GSEventRunModal + 217
      30  GraphicsServices                    0x01db1289 GSEventRun + 115
      31  UIKit                               0x008c5c93 UIApplicationMain + 1160
      32  &lt;app_name&gt;                          0x00002a30 main + 192
      33  &lt;app_name&gt;                          0x00002255 start + 53
  )
  terminate called throwing an exception
  </code></pre>
  
  <p>How can I find out the reason why the app is terminated by this log and solve it? Thank you.</p>
  
  <p><strong>Note:</strong> Edit 1 and Edit 2 were removed.</p>
  
  <p>Edit 3: This implemented code of <code>tableView:numberOfRowsInSection:</code> method works for me:</p>
  
  <pre><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
      if (tableView == self.searchDisplayController.searchResultsTableView)
      {
          if (filteredSectionListOfAddressBook &amp;&amp; filteredSectionListOfAddressBook.count)
          {
              return [[filteredSectionListOfAddressBook objectAtIndex:section] count];
          }
          else
          {
              return 0;
          }
      }
      else if (tableView == self.tableView)
      {
          if (sectionListOfAddressBook &amp;&amp; sectionListOfAddressBook.count)
          {
              return [[sectionListOfAddressBook objectAtIndex:section] count];
          }
          else
          {
              return 0;
          }
      }
  
      return 0;
  }
  </code></pre>