如何访问多级结构化 Plist

Posted

技术标签:

【中文标题】如何访问多级结构化 Plist【英文标题】:How to access multi level structured Plist 【发布时间】:2014-01-28 15:03:25 【问题描述】:

如何从具有这种格式的 plist 中访问特定的周计划? 如果有人知道与多级结构化 plist 相关的任何教程,请提出建议。

以下代码为我提供了存储在 Plist 中的全部数据

-(void)ReadAppPlist

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

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

plist 的格式如下。

 <?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>

我不知道如何检索这些数据,如果有人知道解决方案,请帮助我... 提前致谢。

【问题讨论】:

Run -1 有一个字符串 (Beginner) 用于键 Level,一个字符串 (5K) 用于键 Category,用于键 Plan 的字典等 是的,它包含所有详细信息,例如级别、类别、计划。 【参考方案1】:

Using this answer:

NSString *errorDesc = nil;
NSPropertyListFormat format;
plistPath = [[NSBundle mainBundle] pathForResource:@"runplan" ofType:@"plist"];
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSMutableDictionary *properties = (NSMutableDictionary *)[NSPropertyListSerialization propertyListFromData:plistPath mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];

//Now get the nested dictionary for the key "Run - 1"
NSDictionary *name = (NSDictionary *)[properties valueForKey:@"Run - 1"];

//Now get the nested dictionary for the key "Week 1"
NSDictionary *name2 = (NSDictionary *)[name valueForKey:@"Week 1"];

基本上你可以遍历层次结构来获取valueForKey:的字典。

【讨论】:

name 返回整个 plist & name2 返回空值。 我为密钥使用了错误的字符串。我有“第 1 周”,但您的 pList 是“第 1 周”。已更新。【参考方案2】:

通过这种方式我们可以遍历到我们的plist文件的内部结构...

-(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);

    

【讨论】:

以上是关于如何访问多级结构化 Plist的主要内容,如果未能解决你的问题,请参考以下文章

使用Django ORM查询如何注释是不是存在多级外键层次结构

使用可编码和静态变量的 Plist 结构类

SpringBoot目录结构

如何将一组自定义结构保存到 plist swift

数据库树形结构多级联动的表设计

iOS 动态树形结构 - 实现多级菜单,附带复选框功能