Objective c NSRangeException NSArray objectAtIndex:索引11超出空数组目标C的界限

Objective c NSRangeException NSArray objectAtIndex:索引11超出空数组目标C的界限,objective-c,arrays,uitableview,uisearchbar,Objective C,Arrays,Uitableview,Uisearchbar,我有一个搜索栏,可以在4square Api中搜索场地 有时,当我搜索一个特定的单词并向下滚动时,我的应用程序会在一个空数组中崩溃 下面是我的代码和错误代码 *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 11 beyond bounds for empty array' *** First throw call

我有一个搜索栏,可以在4square Api中搜索场地

有时,当我搜索一个特定的单词并向下滚动时,我的应用程序会在一个空数组中崩溃

下面是我的代码和错误代码

*** 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
这是我的密码

RecipedDetailViewController.h

#import <UIKit/UIKit.h>

@interface RecipeDetailViewController : UIViewController <UITextFieldDelegate, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource, UISearchDisplayDelegate>{
UITableView *searchTableView;
UISearchBar *sBar;
UISearchDisplayController *searchDisplayController;
}

@property (strong, nonatomic) NSArray *loadedSearches;

@end

是否可以尝试在viewDidLoad中初始化LoadedSearchs NSArray?

是否可以粘贴表视图数据源的代码?是否确定该代码中存在错误?我没有看到正在引用的
NSArray
对象。请提供一个符号堆栈跟踪。您能像这样调用
NSArray
objectAtIndex
self.loadedSearches[indexPath.row]
?@AhmedZ-Yes;请参阅本页上的对象订阅:在IndexPath中使用未声明的标识符
#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&client_id=EI10B1E0VF2KGLXL4G5YXQVSNGXD4ABXUHOYN3RWKY3ZUPD0&client_secret=D4BT4LC3MTNULCGLC30YKWITCHIOURNOEXRR04AL3H4YUVKK&v=20121223&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];

}