NSRangeException NSArray objectAtIndex:索引11超出空数组Objective C的边界

时间:2013-05-21 10:02:38

标签: 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 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

这是我的代码

RecipeDetailViewController.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

RecipeDetailViewController.m

#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];

}

1 个答案:

答案 0 :(得分:1)

您可以尝试在viewDidLoad中初始化loadedSearches NSArray吗?