在另一个 nsdictionary 中转换 plist

发布于 2024-12-07 15:05:04 字数 1495 浏览 1 评论 0原文

我不是英语,是 Objective C 的新手,也是这个网站的新手。但我希望你能理解我的问题。我尝试使用搜索引擎,但找不到答案......

问题: 我有一个像这样的 plist 文件

<plist version="1.0">
<array>
<dict>
    <key>ID</key>
    <string>001</string>
    <key>CAT</key>
    <string>A</string>
    <key>TITLE</key>
    <string>AAAA01</string>
</dict>
<dict>
    <key>ID</key>
    <string>002</string>
    <key>CAT</key>
    <string>A</string>
    <key>TITLE</key>
    <string>AAAA02</string>
</dict>
<dict>
    <key>ID</key>
    <string>003</string>
    <key>CAT</key>
    <string>B</string>
    <key>TITLE</key>
    <string>BBBB01</string>
</dict>
.....
</array>
</plist>

,所以我有很多这样的条目。每个条目都有一个类别 (CAT),如我的示例中的“A”和“B”。 现在我需要一个代码将其转换为以下内容:

NSArray
    NSDictionary
        CAT => "A"
        VALUES => {"AAAA01","AAAA02"}
    NSDictionary
        CAT => "B"
        VALUES => {"BBBB01", ...}
    ETC.

我的解决方案:

我使用以下代码获取 plist

NSString *path = [[NSBundle mainBundle] pathForResource:@"Dictonary" ofType:@"plist"];
arrayIndex = [[NSArray alloc] initWithContentsOfFile:path];

现在我已将所有内容保存在 arrayIndex 中,但后来我找不到一种方法来将代码转换为新数组以获得我想要的结果。

请问有人可以帮助我吗?

非常感谢!

I'm not english, new to objective c and new to this website. But i hope you will understand my problem. I tried to use the search engine but i couldn't find a answer...

THE PROBLEM:
i have a plist file like this

<plist version="1.0">
<array>
<dict>
    <key>ID</key>
    <string>001</string>
    <key>CAT</key>
    <string>A</string>
    <key>TITLE</key>
    <string>AAAA01</string>
</dict>
<dict>
    <key>ID</key>
    <string>002</string>
    <key>CAT</key>
    <string>A</string>
    <key>TITLE</key>
    <string>AAAA02</string>
</dict>
<dict>
    <key>ID</key>
    <string>003</string>
    <key>CAT</key>
    <string>B</string>
    <key>TITLE</key>
    <string>BBBB01</string>
</dict>
.....
</array>
</plist>

so i have many entries like this. every entry has a category (CAT) like in my example "A" and "B".
now i need a code to transform this to the following:

NSArray
    NSDictionary
        CAT => "A"
        VALUES => {"AAAA01","AAAA02"}
    NSDictionary
        CAT => "B"
        VALUES => {"BBBB01", ...}
    ETC.

MY SOLUTION:

i get the plist with the following code

NSString *path = [[NSBundle mainBundle] pathForResource:@"Dictonary" ofType:@"plist"];
arrayIndex = [[NSArray alloc] initWithContentsOfFile:path];

Now i have everything saved in the arrayIndex but then i dont find a way to transform the code in a new array to get my wanted result.

please can anybody help me?

THANK YOU very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小霸王臭丫头 2024-12-14 15:05:04

这就是直接编程。

通过两个步骤可以更轻松地完成尝试查找要添加的条目。

// First create a dictionary of CAT and the values,
    NSMutableDictionary *tempDict = [NSMutableDictionary dictionary];
    for (NSDictionary *item in arrayIndex) {
        NSString *cat = [item valueForKey:@"CAT"];

        NSMutableArray *tempArray = [tempDict valueForKey:cat];
        if (!tempArray) {
            tempArray = [NSMutableArray array];
            [tempDict setValue:tempArray forKey:cat];
        }

        NSString *v = [item valueForKey:@"TITLE"];
        [tempArray addObject:v];
    }
    NSLog(@"tempDict: %@", tempDict);
// This may well be a good final result


// This step transforms the dictionary into a list of dictionaries    
    NSMutableArray *newList = [NSMutableArray array];
    NSArray *keys = [[tempDict allKeys] sortedArrayUsingSelector:@selector(compare:)];
    for (NSString *cat in keys) {
        [newList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                            cat, @"CAT",
                            [tempDict valueForKey:cat], @"VALUES",
                            nil]];
    }
    NSLog(@"newList: %@", newList);

This is straight programming.

It is more easily done in two steps saving trying to find an entry to add to.

// First create a dictionary of CAT and the values,
    NSMutableDictionary *tempDict = [NSMutableDictionary dictionary];
    for (NSDictionary *item in arrayIndex) {
        NSString *cat = [item valueForKey:@"CAT"];

        NSMutableArray *tempArray = [tempDict valueForKey:cat];
        if (!tempArray) {
            tempArray = [NSMutableArray array];
            [tempDict setValue:tempArray forKey:cat];
        }

        NSString *v = [item valueForKey:@"TITLE"];
        [tempArray addObject:v];
    }
    NSLog(@"tempDict: %@", tempDict);
// This may well be a good final result


// This step transforms the dictionary into a list of dictionaries    
    NSMutableArray *newList = [NSMutableArray array];
    NSArray *keys = [[tempDict allKeys] sortedArrayUsingSelector:@selector(compare:)];
    for (NSString *cat in keys) {
        [newList addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                            cat, @"CAT",
                            [tempDict valueForKey:cat], @"VALUES",
                            nil]];
    }
    NSLog(@"newList: %@", newList);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文