ios - 如何访问多级结构化 Plist

标签 ios objective-c plist nsmutabledictionary

如何从具有这种格式的 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>

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

最佳答案

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: 的字典。

关于ios - 如何访问多级结构化 Plist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21409158/

相关文章:

iphone - CoreLocation 的准确度测量有多准确

iphone - 加载和卸载消息

ios - 使用 Swift 3 解析 JSON

objective-c - (char **)(&samples) 声明了什么?

iphone - 复制dylib

swift - 在 Swift 中将字典映射到结构体数组

ios - 更新应用程序后在应用程序中添加新数据

iphone - writeToFile 在模拟器上工作,但在设备上不工作

multithreading - 有什么方法可以在不创建伪造的 NSThread 的情况下进入 Cocoa 多线程模式?

objective-c - 使用AFNetworking或JSONKit与Node.js Express网络服务器对话