Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enumerate CFProperyList / CFDictionary keys

I would like to iterate through a CFDictionary (CFPropertyList) and get all values on a specific level.

This would be my dictionary / property-list:


 root
 
  A
  
   foo
   0
   bar
   0
  
  B
  
   foo
   10
   bar
   100
  
  C
  
   foo
   20
   bar
   500
  
 

Using ObjC it would look something like this:

//dict is loaded with the dictionary below "root"
NSDictionary *dict = [...];
NSEnumerator *enumerator = [dict keyEnumerator];
NSString *key;
while (key = [enumerator nextObject]) 
{
    NSLog(key);
};

And it would print out a list of keys to the console like this:

A
B
C

How do you achieve this when using C/C++ on the CoreFoundation-level?

like image 934
Till Avatar asked Feb 17 '10 18:02

Till


1 Answers

Use CFDictionaryApplyFunction to iterate through a dictionary.

static void printKeys (const void* key, const void* value, void* context) {
  CFShow(key);
}
...
CFDictionaryApplyFunction(dict, printKeys, NULL);
like image 120
kennytm Avatar answered Nov 14 '22 02:11

kennytm