Nested NSDictionary from plist

I have a plist with a main NSDictionary in which its keys are dates. For each date, I have an NSDictionary in which the keys are (let's say) categories. Each category contains keys and values.

I would like to create 2 variables, each containing the correct NSDictionary:

NSDictionary *dates = ?
NSDictionary *Categories = ?

      

Below is my plist, please help me understand how this should be done.

** Note. I know how to assign the first dictionary of dates from a plist ... just stuck with Categories.

NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];
self.dates = dict;
[dict release];

      

Plist:

<dict>
    <key>2010-05-08</key>
    <dict>
        <key>Catergory1</key>
        <dict>
            <key>key1</key>
            <string>value1</string>
            <key>key2</key>
            <string>value2</string>
            <key>key3</key>
            <string>value3</string>
        </dict>
        <key>Catergory2</key>
        <dict>
            <key>key1</key>
            <string>value1</string>
            <key>key2</key>
            <string>value2</string>
            <key>key3</key>
            <string>value3</string>
        </dict>
    <key>2010-01-02</key>
    <dict>
        <key>Catergory1</key>
        <dict>
            <key>key1</key>
            <string>value1</string>
            <key>key2</key>
            <string>value2</string>
            <key>key3</key>
            <string>value3</string>
        </dict>
        <key>Catergory2</key>
        <dict>
            <key>key1</key>
            <string>value1</string>
            <key>key2</key>
            <string>value2</string>
            <key>key3</key>
            <string>value3</string>
        </dict>
    </dict>
</dict>
</plist>

      

Any help would be greatly appreciated as I searched the forum history and did not find anything that matches my scenario.

THANKS!

+2


a source to share


1 answer


You have to markup the values ​​as arrays, so you can just iterate over them.

<plist version="1.0">
<dict>
 <key>dates</key>
 <array>
  <dict>
   <key>date</key>
   <date>2010-05-17T12:40:32Z</date>
   <key>categories</key>
   <array>
    <dict>
     <key>key</key>
     <string>value</string>
    </dict>
    <dict>
     <key>key</key>
     <string>value</string>
    </dict>
   </array>
  </dict>
  <dict>
   <key>date</key>
   <date>2010-05-17T12:40:32Z</date>
   <key>categories</key>
   <array>
    <dict>
     <key>key</key>
     <string>value</string>
    </dict>
    <dict>
     <key>key</key>
     <string>value</string>
    </dict>
   </array>
  </dict>
 </array>
</dict>
</plist>

      



the code:

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:pathToFile];
 for (NSDictionary *date in [dict valueForKey:@"dates"]) {
  NSArray *catgories = [date valueForKey:@"categories"];
 }

      

+2


a source







All Articles