read from plist file

Read from .plist File

An Example

tryplist.plist
Key Type Value
Root Dictionary (2 items)
    subroot1 Dictionary (3 items)
        name1 String this is string 1
        name2 Number 1.5
        name3 String this is string a
    subroot2 Dictionary (3 items)
        name1 String this is string 2
        name2 Number 2.5
        name3 String this is string b

Code

create dictionary from file

// get file path
NSString *plistpath = [[NSBundle mainBundle] pathForResource: @"tryplist" ofType: @"plist"];
// create dictionary from file
NSDictionary *plistDictionary = [NSDictionary dictionaryWithContentsOfURL: [NSURL fileURLWithPath: plistpath]];

get data from dictionary

// get subroot1 dictionary
NSDictionary *subroot1Dictionary = [plistDictionary objectForKey: @"subroot1"];
// get name1 of subroot1
NSString *subroot1name1 = [subroot1Dictionary objectForKey: @"name1"];
NSLog(@"%@", subroot1name1);
// get name2 of subroot1
float subroot1name2 = [[subroot1Dictionary objectForKey: @"name2"] floatValue];
NSLog(@"%f", subroot1name2);
// get name3 of subroot1
NSString *subroot1name3 = [subroot1Dictionary objectForKey: @"name3"];
NSLog(@"%@", subroot1name3);
// get subroot2 dictionary
NSDictionary *subroot2Dictionary = [plistDictionary objectForKey: @"subroot2"];
// get name1 of subroot2
NSString *subroot2name1 = [subroot2Dictionary objectForKey: @"name1"];
NSLog(@"%@", subroot2name1);
// get name2 of subroot2
float subroot2name2 = [[subroot2Dictionary objectForKey: @"name2"] floatValue];
NSLog(@"%f", subroot2name2);
// get name3 of subroot2
NSString *subroot2name3 = [subroot2Dictionary objectForKey: @"name3"];
NSLog(@"%@", subroot2name3);

Output

2013-01-02 23:11:28.354 ketmales[6290:c07] this is string 1
2013-01-02 23:11:28.355 ketmales[6290:c07] 1.500000
2013-01-02 23:11:28.355 ketmales[6290:c07] this is string a
2013-01-02 23:11:28.356 ketmales[6290:c07] this is string 2
2013-01-02 23:11:28.356 ketmales[6290:c07] 2.500000
2013-01-02 23:11:28.356 ketmales[6290:c07] this is sting b

posted on 2013-01-02 23:18  紫澜  阅读(307)  评论(0编辑  收藏  举报