Search code examples
objective-cnsstringuilabelnsarray

get string from array to apply to labels


I am fetching json objects. Then filtering down to one object, this results in array of one object. Here is a example:

company
companyname: richs diner
state: iowa
city: antioch

company
companyname: dines
state: california
city: LA

I filter the above to one company. then filter to only apply the city to a label, but it appears you can't change the one word array to a string.

I want to apply each value to a label. But I get the error below. any ideas?

Here is sample code:

     - (void)fetchedData:(NSData *)responseData {

//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:responseData //1
                      options:kNilOptions
                      error:&error];

NSArray* getCompaniesArray = [json objectForKey:@"CompaniesCD"]; //2  get all company info

//NSDictionary* getCompaniesArray = [json objectForKey:@"CompaniesCD"]; //2  get all company info convert to dictionary insted

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"companyName = %@", selectedCompany];//added create filter to only selected state


NSArray *filteredArray = [getCompaniesArray filteredArrayUsingPredicate:predicate];//apply the predicate filter on the array


NSString *city = [filteredArray valueForKey:@"entityformSubmissionID"]; //print array to the string  //error
//NSString *city = [filteredArray objectAtIndex:0];//error
//NSString *city = filteredArray[0];//error

NSLog(@"here is your result: %@", city);//return result.  Works just fine

cityLabel.text = city;  //this does not apply the string to the label results in error

    }

My error is:

[__NSArrayI length]: unrecognized selector sent to instance 0x7fea5405f960 2015-12-28 21:49:58.027 J[3203:933300] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x7fea5405f960'


Solution

  • Access to array with index. cant you access to array with key.

    NSString *city = filteredArray[0];//index 0 or other index 
    cityLabel.text = city;