ios - Getting the error NSCFDictionary objectAtIndex while displaying data in Uitableview -
i new ios , when implementing fetch data api , getting -[__nscfdictionary objectatindex:]:
error.
code:
request dispatching
-(void)jobtakefive{ [mbprogresshud showhudaddedto:self.view animated:yes]; //nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:[nsstring stringwithformat:@"http://www.json-generator.com/api/json/get/cucmzxkuoa?indent=2"]]]; nsurlrequest *request = [nsurlrequest requestwithurl:[nsurl urlwithstring:[nsstring stringwithformat:@"http://www.json-generator.com/api/json/get/cpviemqesy?indent=2"]]]; nslog(@"%@",request); nsurlconnection *conn = [[nsurlconnection alloc] initwithrequest:request delegate:self]; if (conn) { responsedata = [[nsmutabledata alloc] init]; }else{ } }
response handling , parsing
- (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response { [responsedata setlength:0]; postresponce = [[nsmutabledata alloc] init]; [postresponce setlength:0]; } - (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { [responsedata appenddata:data]; [postresponce appenddata:data]; } -(void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error{ nslog(@"%@ ", [error localizeddescription]); } - (void)connectiondidfinishloading:(nsurlconnection *)connection { nserror *error=nil; nsdictionary *responce =[nsjsonserialization jsonobjectwithdata:responsedata options:kniloptions error:&error]; resparray=[responce valueforkey:@"yes_questions"]; resparray1=[responce valueforkey:@"no_questions"]; [mbprogresshud hidehudforview:self.view animated:yes]; [take5table reloaddata]; }
troublezone - showing response in tableview
-(nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 2; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { switch (section) { case 0: return resparray.count; break; case 1: return resparray1.count; break; default: return 0; } } -(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath{ [tableview deselectrowatindexpath:indexpath animated:yes]; uitableviewcell *cell = [tableview cellforrowatindexpath:indexpath]; nslog(@"section:%ld row:%ld selected , data %@", (long)indexpath.section,(long)indexpath.row,cell.textlabel.text); } - (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section{ if (section == 0) return @"first section header title"; if (section == 1) return @"second section header title"; else return @"hello world"; } -(uitableviewcell*)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cellidentifier1"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (!cell) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; cell.accessorytype = uitableviewcellaccessorydisclosureindicator; } uilabel *sno = (uilabel *)[cell viewwithtag:96]; uilabel *queslab = (uilabel *)[cell viewwithtag:98]; if (indexpath.section==0) { // titleuil.text = [[dicc valueforkey:[headersarray objectatindex:indexpath.section]]objectatindex:indexpath.row]; sno.text=[nsstring stringwithformat:@"%@",[[resparray objectatindex:indexpath.row] valueforkey:@"question_id"]]; queslab.text=[[resparray objectatindex:indexpath.row]valueforkey:@"question_title"]; } else if (indexpath.section==1){ sno.text=[nsstring stringwithformat:@"%@",[[resparray1 objectatindex:indexpath.row] valueforkey:@"question_id"]]; queslab.text=[[resparray1 objectatindex:indexpath.row]valueforkey:@"question_title"]; } if (indexpath.row %2 ==1) cell.backgroundcolor = [uicolor colorwithred:.9 green:.9 blue:.9 alpha:1]; else cell.backgroundcolor = [uicolor colorwithred:.8 green:.8 blue:.8 alpha:1]; return cell; } -(cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath { return 50; }
error
-[__nscfdictionary objectatindex:]: unrecognized selector sent instance 0x7f90d3eaf8a0 2016-11-08 10:44:16.935 ecs services[3691:701820] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[__nscfdictionary objectatindex:]: unrecognized selector sent instance 0x7f90d3eaf8a0'
my response is:
printing description of responce: { "job_location" = "crown promenade "; "job_name" = "test schedule 2 crown promenade"; "job_no" = 207; "no_questions" = { 12 = { "question_answer" = no; "question_id" = 13; "question_title" = "is there risk of falls or falling objects?"; "question_type" = full; }; 13 = { "question_answer" = no; "question_id" = 14; "question_title" = "is there risk of plant or traffic interaction?"; "question_type" = full; }; 14 = { "question_answer" = no; "question_id" = 15; "question_title" = " there risk of electrical hazards?"; "question_type" = full; }; 15 = { "question_answer" = no; "question_id" = 16; "question_title" = " work in or involve confined space? "; "question_type" = full; }; 16 = { "question_answer" = no; "question_id" = 19; "question_title" = " job involve hot works?"; "question_type" = short; }; 17 = { "question_answer" = no; "question_id" = 20; "question_title" = "is there risk of environmental damage? "; "question_type" = full; }; 18 = { "question_answer" = no; "question_id" = 21; "question_title" = " there risk of crush injuries? (in,on or between)"; "question_type" = full; }; 19 = { "question_answer" = no; "question_id" = 22; "question_title" = "is there risk of water or sun hazards?"; "question_type" = full; }; 20 = { "question_answer" = no; "question_id" = 23; "question_title" = "are there access issues or concerns "; "question_type" = full; }; 21 = { "question_answer" = no; "question_id" = 24; "question_title" = " there hazards involving unstable ground?"; "question_type" = full; }; 22 = { "question_answer" = no; "question_id" = 25; "question_title" = " need apply isolation ?.................... "; "question_type" = short; }; 23 = { "question_answer" = no; "question_id" = 26; "question_title" = "could there uncontrolled movement? \u2026...\u2026\u2026.\u2026.. "; "question_type" = full; }; 24 = { "question_answer" = no; "question_id" = 27; "question_title" = "do need mechanical lifting or work @ height platform? .. "; "question_type" = full; }; 25 = { "question_answer" = no; "question_id" = 39; "question_title" = test; "question_type" = full; }; }; "yes_questions" = { 0 = { "question_answer" = yes; "question_id" = 1; "question_title" = " understand job thoroughly?"; "question_type" = full; }; 1 = { "question_answer" = yes; "question_id" = 2; "question_title" = "am authorized, qualified & trained work?"; "question_type" = short; }; 10 = { "question_answer" = yes; "question_id" = 11; "question_title" = "do have appropriate ppe & in condition?"; "question_type" = full; }; 11 = { "question_answer" = yes; "question_id" = 12; "question_title" = "can complete task on own ? "; "question_type" = full; }; 2 = { "question_answer" = yes; "question_id" = 3; "question_title" = "do have correct tools & they\u2019re in condition?"; "question_type" = full; }; 26 = { "question_answer" = yes; "question_id" = 40; "question_title" = ""; "question_type" = full; }; 27 = { "question_answer" = yes; "question_id" = 41; "question_title" = "equipment in condition"; "question_type" = full; }; 3 = { "question_answer" = yes; "question_id" = 4; "question_title" = "have informed others may affected work? "; "question_type" = full; }; 4 = { "question_answer" = yes; "question_id" = 5; "question_title" = "is there free access others not involved in vicinity?"; "question_type" = full; }; 5 = { "question_answer" = yes; "question_id" = 6; "question_title" = " have read & understand swms applicable task? "; "question_type" = full; }; 6 = { "question_answer" = yes; "question_id" = 7; "question_title" = "have obtained permits & filled out correct paperwork? "; "question_type" = short; }; 7 = { "question_answer" = yes; "question_id" = 8; "question_title" = "have applied lock out tags (where & if required)? "; "question_type" = full; }; 8 = { "question_answer" = yes; "question_id" = 9; "question_title" = " have performed pre job on-site inspection?"; "question_type" = full; }; 9 = { "question_answer" = yes; "question_id" = 10; "question_title" = " fit task? (fatigue, injury, medications)"; "question_type" = full; }; }; }
the problem is, response returning dictionaries, not arrays expecting. fix that, can use allvalues
method of nsdictionary returns array of values inside dictionary.
to fix that, make following corrections:
in response parsing:
- (void)connectiondidfinishloading:(nsurlconnection *)connection { nserror *error=nil; nsdictionary *responce =[nsjsonserialization jsonobjectwithdata:responsedata options:kniloptions error:&error]; resparray=[[responce valueforkey:@"yes_questions"] allvalues]; //now have array of dictionaries contained in dictionary key yes_question resparray1=[[responce valueforkey:@"no_questions"] allvalues];//now have array of dictionaries contained in dictionary key no_question [mbprogresshud hidehudforview:self.view animated:yes]; [take5table reloaddata]; }
Comments
Post a Comment