views:

98

answers:

2

Hello,

I have a plist, with main NSDictionary, in which its keys are dates. For every date, I have a NSDictionary in which the keys are (let's say) categories. Every Category holds Keys and values.

I would like to create 2 variables that each will hold the correct NSDictionary:

NSDictionary *dates = ?
NSDictionary *Categories = ?

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

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

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

The 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've searched over the forum's history and found nothing that matches my scenario.

THANKS!

A: 

You should markup the values as arrays, so you can simply 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>

Code:

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:pathToFile];
 for (NSDictionary *date in [dict valueForKey:@"dates"]) {
  NSArray *catgories = [date valueForKey:@"categories"];
 }
unset
Hey, honestly, adding arrays just made it hard for me to read/understand,let's go back to my plist for a sec,i just want to have the following:1 NSDictionary with all the dates1 NSdictionary with all the Categoriesthen i could ask, [category objectForKey:@"Key1"] and i'll get Value1,any ideas?
Dror Sabbag
The problem is, that you have several "Category"-Dictionaries, and I think it could cause issues, if you mix them down to one Dictionary. On the other hand: What's the problem with `[catgories objectAtIndex:0]` in my suggestion?
unset
Hey unset, i got your point, and you are right, i will use the [categories objectAtIndex:row] and will loop inside the main Dictionary.Thanks for your help
Dror Sabbag
A: 

Hi !

I'm beginner and I try to understand... HOW I can make a request to show all data (from plist) in SAME category (NOT ALL !) for exemple : show only the movie have : genre = Action

Please help me ... Thanks a lot ! Excuse my bad english !

Is it in this code I must make it or an other ???

  • (id)initWithLibraryName:(NSString *)libraryName { if (self = [super init]) { libraryPlist = libraryName; libraryContent = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:libraryPlist ofType:@"plist"]];
Fraise
Hey, Better ask it as a new question and not here as an answer.Dror.
Dror Sabbag