NSRangeException NSArray objectAtIndex: index 11 beyond bounds for empty array Objective C -
i have search bar searches 4square api venues.
sometimes when search specific word , scroll down app crashes empty array.
below code , error code.
*** terminating app due uncaught exception 'nsrangeexception', reason: '*** -[__nsarrayi objectatindex:]: index 11 beyond bounds 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 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 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 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 tableview when block gets 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 tapped table cell" delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil, nil]; [alert show]; [tableview deselectrowatindexpath:indexpath animated:yes]; }
can try initializing loadedsearches nsarray in viewdidload?
Comments
Post a Comment