0

I have a search bar that searches the 4square Api for venues.

Sometimes when I search for a specific word and scroll down my app crashes with a empty array.

Below is my code and error 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

Here is my code

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

}
7
  • Can you paste the code of the table view datasource? Commented May 21, 2013 at 10:04
  • You're certain the error is in that code? I don't see an NSArray object being referenced. Please provide a symbolic stacktrace. Commented May 21, 2013 at 10:04
  • can you call an objectAtIndex for NSArray like this self.loadedSearches[indexPath.row] ? Commented May 21, 2013 at 10:16
  • 1
    @AhmedZ Yes; see Object Subscripting on this page: clang.llvm.org/docs/ObjectiveCLiterals.html Commented May 21, 2013 at 10:17
  • Getting use of undeclared identifier at IndexPath Commented May 21, 2013 at 10:18

1 Answer 1

1

Can you try initializing loadedSearches NSArray in viewDidLoad?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.