0

this surely is a pretty basic question, but i've been struggling and still no answer.

I have an array of dictionaries, each NSdictionary has the same amount and type of key-value pairs, i want to store all the values(NSstring) of a given key (all dictionaries have that same key) but still no luck.

the last thing i come up with was:

int i=0;
while (i< dictArray.count) {
            [newArray addObject:[dictArray[i] objectForKey:@"keyName"]];
        NSLog(@"%@",[dictArray[i] objectForKey:@"keyName"]);
        NSLog(@"%@",newArray);
        i++;
    }

In the NSlogs i put for debugging, i can see that [rows[i] objectForKey:@"keyName"] displays te correct NSstring value for "keyName", but the Newarray NSlog shows null, what am i doing wrong? is there a better way to get this valúes? thank you!

PD: newArray is a NSMutaleArray

4
  • U have tocreate an array with the values in the dictionary according to the key values?, U should use value for key Commented Mar 20, 2014 at 12:51
  • Have initialized newArray as a nsmutablearray? newArray =[NSMUtableArray array];? Commented Mar 20, 2014 at 12:53
  • yes, newArrray is a NSMutableArray Commented Mar 20, 2014 at 13:01
  • is it allocated and initialized? newArray =[NSMUtableArray array];? Commented Mar 20, 2014 at 13:12

3 Answers 3

1

I consider the newArray is a Mutable Array. You can do that :

 [dictArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
             [newArray addObject:[obj objectForKey:@"keyName"]];
        }];
Sign up to request clarification or add additional context in comments.

2 Comments

This is the best answer so far. +1 :)
I may be looking wrong...but it isn't a ] missing somewhere?
0

Iterate over the array which has dictionaries..

//Initialze the array
NSMutableArray *newArray = [[NSMutableArray alloc]init];
//For Loop
for (NSMutableDictionary *dict in dictArray) {
[newArray addObject:[dict objectForKey:@"keyName"]];
}

Comments

0

It seems your newArray is NSMutableArray object. Did you allocate the newArray before the while loop? If no, please do this before the loop

newArray = [[NSMutableArray alloc] init];

you will be okay.

3 Comments

i've got my newArray initialized as a NSMutableArray since is a property of my class
So you have put the exact line somewhere before the loop starts right?
i thought that, as a property, the NSMutableArray was allocated and initialized, i see my mistake now, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.