user973067
user973067

Reputation: 337

iPhone:how to get a value from NSMutable Array with keys

I would like to know how to fetch a value from a NSMutableArray with keys. The array is built like this:

    qtype = [[NSMutableArray alloc] init];
    while (dicjson = (NSDictionary*)[enumerator nextObject]) {
     question_id = [dicjson objectForKey:@"question_id"];        
     question_text = [dicjson objectForKey:@"question_text"];
     question_type = [dicjson objectForKey:@"question_type"];

     [qtype addObject:[NSDictionary dictionaryWithObject:question_type forKey:question_id]];

} 

I am populating the array in a table cell:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *type = [qtype objectAtIndex:[indexPath row]];

}

however the output is like "65 = YN" which is not what i want. I would like to extract only "65". If you could give me some idea, would be very much appreciated.

Upvotes: 0

Views: 556

Answers (4)

Manish Agrawal
Manish Agrawal

Reputation: 11026

you can access each object of array which is of NSDictionary type.Following is the code for that

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = [qtype objectAtIndex:[indexPath row]];
    NSString *question_id = [dict objectForKey:@"question_id"];     
    NSString *question_text = [dict objectForKey:@"question_text"];
    NSString *question_type = [dict objectForKey:@"question_type"];
}

Upvotes: 0

Tomasz Wojtkowiak
Tomasz Wojtkowiak

Reputation: 4920

In the fact, I really don't understand why you use an array of dictionaries.
You can simply build array of question texts:

qtype = [[NSMutableArray alloc] init];
while (dicjson = (NSDictionary*)[enumerator nextObject]) {
 question_id = [dicjson objectForKey:@"question_id"];        
 question_text = [dicjson objectForKey:@"question_text"];
 question_type = [dicjson objectForKey:@"question_type"];

 [qtype addObject:question_id];

 } 

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *type = [qtype objectAtIndex:[indexPath row]];

}

But if you need qtype for other purposes, you can get

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = [qtype objectAtIndex:[indexPath row]];
    NSString *type = [[dict allKeys] objectAtIndex:0]; 

}

Upvotes: 1

user971401
user971401

Reputation:

It seems the dictionary for the question type contains only one key, so you can achieve what you want, but remember : dictionaries are unordered collections working with keys, whereas arrays are ordered collections working with indexes.

Anyway, in tableView:cellForRowAtIndexPath:, modify the code to get the only key (which is the question id) in the dictionary :

-(UITableViewCell *)tableView:(UITableView *)tableView
        cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *dictKeys = [[qtype objectAtIndex:[indexPath row]] allKeys];
    NSString *type = [dictKeys objectAtIndex:0];
}

Upvotes: 1

lukewar
lukewar

Reputation: 2715

It is because you store NSDictionary in "qtype". Try:

NSDictionary *dict = [qtype objectAtIndex:[indexPath row]];
NSString *type = [[dict allValues] lastObject];//since you have only one object stored.

Upvotes: 0

Related Questions