read multi-array from plist

I can't get it work to have the 8 items from the plist below. I have a plist "settings.plist". Now I wan't from vragen -> category (self.catagorie - 1) -> question. The 8 items. But the array keeps empty.

What am I doing wrong?

Code:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"instellingen.plist"];
NSMutableDictionary *instellingen = [NSMutableDictionary dictionaryWithContentsOfFile:path];

NSArray *vragen = [instellingen objectForKey:@"vragen"];

NSArray *cata = [vragen objectAtIndex: (self.catagorie-1)];
NSArray *question = [cata objectAtIndex: 0];
NSLog(@"count: %i", [question count]);

Plist file:

<?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>vragen</key>
    <array>
        <array> <--catagorydata
            <array> <-- questiondata
                <string>vraag</string>
                <string>A</string>
                <string>B</string>
                <string>C</string>
                <string>D</string>
                <string>1</string>
                <string>standaard</string>
                <string></string>
            </array>
            <array>
                <string>vraag2</string>
                <string>A</string>
                <string>B</string>
                <string>C</string>
                <string>D</string>
                <string>1</string>
                <string>afbeelding</string>
                <string>afbeelding.jpg</string>
            </array>
        </array>
    </array>
    <key>firstBoot</key>
    <true/>
    <key>regelAtRuntime</key>
    <true/>
</dict>
</plist>

Answers


First of all this line

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"instellingen.plist"]; ?

should be

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"settings.plist"];

I also had to delete these from the dictionary:

<--catagorydata
<-- questiondata  

Besides the two things above, there is nothing wrong with your code or your dictionary. After making those changes I tested your code and the output I got was:

tester[69637:c07] size of dict 3  
tester[69637:c07] count: 8  

which is correct according to the .plist file

So your problem is at the first 4 lines. Make sure your file is in Documents directory (use something like the following code to see whats going wrong)

    NSLog(@"Path : %@",path);

I copied the file in the project folder (because I used the simulator) and used the following code and everything worked fine

NSBundle* b=[NSBundle mainBundle];
NSURL* path1=[b URLForResource:@"settings" withExtension:@"plist"];
NSMutableDictionary *instellingen = [NSMutableDictionary dictionaryWithContentsOfURL:path1];

NSLog(@"size of dict %d",[instellingen count]);

NSArray *vragen = [instellingen objectForKey:@"vragen"];

NSArray *cata = [vragen objectAtIndex: 0];
NSArray *question = [cata objectAtIndex: 0];
NSLog(@"count: %i", [question count]);

Just a quick read, but looks like you have an array of "categoryData" items, that contains an array of questionData items; questionData items are strings.

NSArray *vragen = [instellingen objectForKey:@"vragen"]; // count = 1
NSArray *cata = [vragen objectAtIndex: (self.catagorie-1)]; // count = 2
NSString *question = [cata objectAtIndex: 0];

Recommend you add some breakpoints and look at your data. OR, NSLog(@"data = %@", cata); etc.


Need Your Help

Get Month & Day from Date

linux unix date

I am trying to get Month and Date from Date in Linux. this is my code

WeakHashMap - what is its purpose and how should it be used correctly

java caching weak-references weakhashmap

Today I found this blog post which discussed usages of WeakHashMap over cache. It was intrigued by the fact that not the values, but the keys are stored as weak references, and when the reference ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.