首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSRangeException NSArray objectAtIndex:空数组目标C的超出边界的索引11

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

Stack Overflow用户
提问于 2013-05-21 10:02:37
回答 1查看 1.2K关注 0票数 0

我有一个搜索栏,搜索4平方Api的场地。

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

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

代码语言:javascript
复制
*** 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

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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];

}
EN

回答 1

Stack Overflow用户

发布于 2013-05-21 13:44:29

您能否尝试在loadedSearches NSArray中初始化viewDidLoad?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16667281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档