Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27191

Parse plist file and get value for key

I have this file below:

enter image description here

I have a question, how can I get url value from this plist file.

I have convert this file to a NSDictionary and it has all keys and values.

I've added this line to check how it works

 NSLog(@"%@", [[[[source objectForKey:@"Root"] objectForKey:@"updates"] objectForKey:@"Item 0"] objectForKey:@"url"]);

but it returns (null) instead of someurl1.

source - this is my NSDictionary instance variable.

This log for my NSDictionary object

(NSDictionary *) $1 = 0x0ab38b60 {
    plist =     {
        dict =         {
            array =             {
                dict =                 (
                                        {
                        integer = 4;
                        key =                         (
                            id,
                            url,
                            compression
                        );
                        string = "http://someurl1";
                        true = "";
                    },
                                        {
                        integer = 5;
                        key =                         (
                            id,
                            url,
                            compression
                        );
                        string = "http://someurl2";
                        true = "";
                    }
                );
            };
            key = updates;
        };
        version = "1.0";
    };
}

Upvotes: 1

Views: 2665

Answers (2)

Mohannad A. Hassan
Mohannad A. Hassan

Reputation: 1648

Very well.

Try this code.

NSDictionary *rootDictionary = [NSDictionary dictionaryWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"PListFile" ofType:@"plist"]];
NSLog(@"%@", [[[rootDictionary objectForKey:@"updates"] objectAtIndex:0] objectForKey:@"url"]);

Upvotes: 2

Wain
Wain

Reputation: 119041

Try:

for (NSDictionary *item in [source objectForKey:@"updates"]) {

    NSLog(@"%@", [item objectForKey:@"url"]);
}

Note that 'Root' isn't a key, it's just the way Xcode displays the plist. And you need to loop over an array, again 'Item 0' is just the way it's displayed in the IDE.

Upvotes: 4

Related Questions