从plist嵌套的NSDictionary [英] nested NSDictionary from plist

查看:86
本文介绍了从plist嵌套的NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个plist,带有主要的NSDictionary,其键为日期. 对于每个日期,我都有一个NSDictionary,其中的键是(比方说)类别. 每个类别都包含键和值.

I have a plist, with main NSDictionary, in which its keys are dates. For every date, I have a NSDictionary in which the keys are (let's say) categories. Every Category holds Keys and values.

我想创建2个变量,每个变量将保存正确的NSDictionary:

I would like to create 2 variables that each will hold the correct NSDictionary:

NSDictionary *dates = ?
NSDictionary *Categories = ?

以下是我的列表,请帮助了解如何完成此操作.

Below is my plist, please help to understand how this should be done.

**注意:我确实知道如何从plist中分配第一个日期字典... 只是停留在类别上.

**Note: I do know how to assign the first dates dictionary from the plist... just stuck with the Categories.

NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];
self.dates = dict;
[dict release];

plist:

<dict>
    <key>2010-05-08</key>
    <dict>
        <key>Catergory1</key>
        <dict>
            <key>key1</key>
            <string>value1</string>
            <key>key2</key>
            <string>value2</string>
            <key>key3</key>
            <string>value3</string>
        </dict>
        <key>Catergory2</key>
        <dict>
            <key>key1</key>
            <string>value1</string>
            <key>key2</key>
            <string>value2</string>
            <key>key3</key>
            <string>value3</string>
        </dict>
    <key>2010-01-02</key>
    <dict>
        <key>Catergory1</key>
        <dict>
            <key>key1</key>
            <string>value1</string>
            <key>key2</key>
            <string>value2</string>
            <key>key3</key>
            <string>value3</string>
        </dict>
        <key>Catergory2</key>
        <dict>
            <key>key1</key>
            <string>value1</string>
            <key>key2</key>
            <string>value2</string>
            <key>key3</key>
            <string>value3</string>
        </dict>
    </dict>
</dict>
</plist>

任何帮助将不胜感激,因为我搜索了论坛的历史记录,但没有找到与我的情况相符的内容.

Any help would be greatly appreciated, as I've searched over the forum's history and found nothing that matches my scenario.

谢谢!

推荐答案

您应该将这些值标记为数组,这样就可以简单地遍历它们.

You should markup the values as arrays, so you can simply iterate over them.

<plist version="1.0">
<dict>
 <key>dates</key>
 <array>
  <dict>
   <key>date</key>
   <date>2010-05-17T12:40:32Z</date>
   <key>categories</key>
   <array>
    <dict>
     <key>key</key>
     <string>value</string>
    </dict>
    <dict>
     <key>key</key>
     <string>value</string>
    </dict>
   </array>
  </dict>
  <dict>
   <key>date</key>
   <date>2010-05-17T12:40:32Z</date>
   <key>categories</key>
   <array>
    <dict>
     <key>key</key>
     <string>value</string>
    </dict>
    <dict>
     <key>key</key>
     <string>value</string>
    </dict>
   </array>
  </dict>
 </array>
</dict>
</plist>

代码:

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:pathToFile];
 for (NSDictionary *date in [dict valueForKey:@"dates"]) {
  NSArray *catgories = [date valueForKey:@"categories"];
 }

这篇关于从plist嵌套的NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆