0

I am having trouble populating an array of objects (employee names) from a group of NSDictionaries (each one representing an individual employee) which are inside another NSDictionary *dictionary (personnel). The key for each sub Dictionary is an employee ID.

I might well be coming at this from a completely wrong direction, i am very much a newbie but from what i have read it should be something like this , i have managed to get as far as a pName (employee name) from each sub Dictionary, all be it fleetingly. I just can't get the pNames added into MutableArray *names.

any help or guidance would be greatly appreciated ...

  int i, count;
  id key, value;

  keys = [dictionary allKeys];
  count = [keys count];
  for (i = 0; i < count; i++)
  {
   key = [keys objectAtIndex: i];
   value = [dictionary objectForKey: key];
   NSLog (@"value:%@", value); 

   NSString *pName = [value objectForKey:@"personName"];
   NSLog (@"pName:%@", pName);// the debugger shows the correct pName with each loop

   [names addObject:pName];// this is wrong and i don't know why
  }
   NSLog (@"names:%@", names);// in the debugger names (null)
2
  • Welcome to Stack Overflow. In future please take the time to format the code in your question appropriately (using the "{}" editor control). Commented Jan 23, 2011 at 11:38
  • Any chance you could update your question with a slightly more fleshed out code sample? (One that shows how the names NSMutableArray is initialised, etc.) Commented Jan 23, 2011 at 11:41

2 Answers 2

3

You can also do this using collection operators, specifically the @unionOfObjects operator:

NSMutableArray *names = [[[personnel allValues] 
                            valueForKeyPath:@"@unionOfObjects.personName"] 
                            mutableCopy];

Note that names will be owned by the method (its retain count is 1), but the interim arrays ([personnel allValues] and the one created by valueForKeyPath:) are autoreleased and not owned by the method.

For some reason (possibly because key names beginning with "@" are special in valueForKey:, and thus valueForKeyPath: wouldn't be equivalent to a sequence of calls to valueForKey:), NSDictionary doesn't have a valueForKeyPath: method, so you first need to convert it to an array using allValues. Alternatively, you could implement valueForKeyPath: in a category, perhaps starting from GnuStep's or cocotron's (which doesn't actually support @unionOfObjects, but allows for easy extension by defining a _kvo_operator_unionOfObjects: method).

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like you aren't creating an array object to put your names in. There needs to be code like this:

names = [NSMutableArray array];

... before the start of the loop. That creates an empty mutable array.

2 Comments

thank you very much .. i appreciate you solving such an elementary issue.
+1 Damn. There was me thinking he'd simply omitted that part of the code sample. :-)

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.