Search code examples
iosobjective-cplistnsmutabledictionary

How to access multi level structured Plist


How can I access a particular week plan from a plist with this format? If any one knows any tutorial related to multilevel structured plist please suggest.

The following code gives me the entire data stored in Plist

-(void)ReadAppPlist
{
    plistPath = [[NSBundle mainBundle] pathForResource:@"runplan" ofType:@"plist"];
    NSMutableDictionary * propertyDict = [[NSMutableDictionary alloc]    initWithContentsOfFile:plistPath];
    name = [propertyDict objectForKey:@"Run - 1"];

    NSLog(@"%@",name.description);  
}

The plist is in the format given below.

 <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>Run - 1</key>
        <dict>
            <key>Level</key>
            <string>Beginner</string>
            <key>Category</key>
            <string>5K</string>
            <key>Plan</key>
            <dict>
                <key>Duration</key>
                <string>5week</string>
                <key>Week 1</key>
                <dict>
                    <key>Day 1</key>
                    <string>Rest or run/walk</string>
                    <key>Day 2</key>
                    <string>2.5 km run</string>
                    <key>Day 3</key>
                    <string>Rest or run/walk</string>
                    <key>Day 4</key>
                    <string>2.5 km run</string>
                    <key>Day 5</key>
                    <string>Rest</string>
                    <key>Day 6</key>
                    <string>2.5 km run</string>
                    <key>Day 7</key>
                    <string>30 - 60 min walk</string>
                </dict>
                <key>Week 2</key>
                <dict>
                    <key>Day 1</key>
                    <string>Rest or run/walk</string>
                    <key>Day 2</key>
                    <string>3 km run</string>
                    <key>Day 3</key>
                    <string>Rest or run/walk</string>
                    <key>Day 4</key>
                    <string>2.5 km run</string>
                    <key>Day 5</key>
                    <string>Rest</string>
                    <key>Day 6</key>
                    <string>3 km run</string>
                    <key>Day 7</key>
                    <string>35 - 60 min walk</string>
                </dict>
            </dict>
        </dict>

I don't know how to retrieve those data so kindly please help me if any one knows the solution... Thanks in advance.


Solution

  • By this way we can traverse to the inner structure of our plist file...

    -(void)ReadAppPlist{
            plistPath = [[NSBundle mainBundle] pathForResource:@"runplan" ofType:@"plist"];
            NSMutableDictionary * propertyDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
            //name1 dictionary will have the entire content of plist
            NSDictionary *name1 = (NSDictionary *)[propertyDict objectForKey:@"Run - 1"];
            // here we are traversing to inner structure of elements.
            NSDictionary *level=[name1 objectForKey:@"Level"];
            NSDictionary *category=[name1 objectForKey:@"Category"];
            NSDictionary *plan=[name1 objectForKey:@"Plan"];
            NSDictionary *week=[plan objectForKey:@"Week 1"];
            NSDictionary *Day=[week objectForKey:@"Day 1"];
    
            NSLog(@"LEVEL %@",level);
            NSLog(@"CATEGORY %@",category);
            NSLog(@"Week Plan %@",week);
            NSLog(@"Day Plan %@",Day);
    
        }