objective c - Array reduplication not working -


xcode problem. have nsmutablearray pulling json objects. example company: name, state, address, phone number, etc.

i pulling info fine. , want display 1 of each state in table view. works fine shows multibles of same state. want show 1 of each state. using code not return states. have seen lot of examples , should work returns nothing. if skip code below show states. have tried loop. , tried array nsset. nothing working. ideas??? problem code...

    //create states array...remove duplicate states here nsarray *statesarray = [companies valueforkeypath:@"@distinctunionofobjects.state"];  return statesarray; 

here whole code. please been struggling week.

@interface statestableviewcontroller ()  @property (nonatomic) nsstring *name; @property (nonatomic) nsstring *state;  @end  @implementation statestableviewcontroller  nsarray *companies; nsarray *statesarray;  - (void)viewdidload {     [super viewdidload];     nsstring *address = @"http://www.companiesfeed";     nsurl *url = [[nsurl alloc] initwithstring:address];      //laod data on background queue..     //if connecting online url need     dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{         companies = [self readcompanies:url];//, statesset = [self readcompanies:url];          //now have data, reload table data on main ui thread         [self.tableview performselectoronmainthread:@selector(reloaddata) withobject:nil waituntildone:yes];     }); }  //new code - (nsarray *)readcompanies:(nsurl *)url {     //create nsurlrequest given url     nsurlrequest *request = [nsurlrequest requestwithurl:url cachepolicy:                              nsurlrequestreloadignoringlocalandremotecachedata timeoutinterval:30.0];      //get data     nsurlresponse *response;     nsdata *data = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:nil];      //now create nsdictionary json data     nsdictionary *jsondictionary = [nsjsonserialization jsonobjectwithdata:data                                                                    options:0 error:nil];      //create new array hold comanies     nsmutablearray *companies = [[nsmutablearray alloc] init];      //get array of dictionaries key "company"     nsarray *array = [jsondictionary objectforkey:@"companies"];      //iterate throught array of dictionaries     (nsdictionary *dict in array) {         //create new company object information in dictionary         company *company = [[company alloc] initwithjsondictionary:dict];          //add company object array         [companies addobject:company ];      }       //create states array...remove duplicate states here     nsarray *statesarray = [companies valueforkeypath:@"@distinctunionofobjects.state"];      return statesarray;      //return companies; }  - (void)didreceivememorywarning {     [super didreceivememorywarning];     // dispose of resources can recreated. }  #pragma mark -table view controller methods //change uniquevalue errors companies  -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger) section {         //return[companies count];     return [statesarray count]; }  - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {     static nsstring *cellid = @"cellidstate";     uitableviewcell *cell = [self.tableview dequeuereusablecellwithidentifier:cellid];      if (cell == nil){         //single line on table view         //cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylevalue1 reuseidentifier:cellid];         // dual line on table view         cell = [[uitableviewcell alloc]                       initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellid];     }      //edit single state     //company *company = [companies objectatindex:indexpath.row];     company *company = [statesarray objectatindex:indexpath.row];      //cell.textlabel.text = company.company_id;     cell.textlabel.text = company.state;     cell.detailtextlabel.text = [nsstring stringwithformat:@"%@",company.companyname];     //adds cheveron tableviewl     [cell setaccessorytype: uitableviewcellaccessorydisclosureindicator];      return cell; }  #pragma mark - navigation -(void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender{  }  @end 

i able unique list of states using predicate , block check set existing states. so:

nsmutablearray *statesarray = [nsmutablearray array];     [companies filteredarrayusingpredicate:[nspredicate predicatewithblock:^bool(id evaluatedobject, nsdictionary *bindings) {         company *dict = (company *)evaluatedobject;         bool seen = [statesarray containsobject:dict];         if (!seen) {             [statesarray addobject:dict];         }         return !seen;     }]];     nslog(@"unique states:  %@", statesarray); 

where companies array looks this:

{[{   "company_id": "22",   "entityformsubmissionid": "22",   "companyname": "house of waffles",   "companyslogan": " home is!",   "address1": "123 here",   "address2": "thre",   "city": "everywhere",   "state": "ca",   "zipcode": "94531",   "phonenumber": "777-777-7777",   "email": "test@test.com",   "additionalcompanyinfo": "all additional company info going here",   "sales": "there no sales!",   "rentalterms": "terms dont break our stuff jerks!",   "companylogo": "/16x16icon.png" }] 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -