Objective C,使用 plist 的正确部分并循环获取数据

发布于 2024-11-19 06:42:28 字数 2125 浏览 2 评论 0原文

对你来说另一个有趣的(可能非常简单)问题,我已经解决了一半,现在陷入了死胡同......

我需要使用 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>Categories</key>
    <array>
        <dict>
            <key>CategoryName</key>
            <string>Test Category</string>
            <key>CategoryID</key>
            <integer>10</integer>
            <key>Sections</key>
            <dict>
                <key>A</key>
                <array>
                    <string>A Jones</string>
                    <string>A King</string>
                </array>
                <key>T</key>
                <array>
                    <string>T Jones</string>
                    <string>T King</string>
                </array>
            </dict>
        </dict>
        <dict>
            <key>CategoryName</key>
            <string>Another Test Category</string>
            <key>CategoryID</key>
            <integer>20</integer>
            <key>Sections</key>
            <dict>
                <key>P</key>
                <array>
                    <string>P Jones</string>
                    <string>P King</string>
                </array>
                <key>S</key>
                <array>
                    <string>S Jones</string>
                    <string>S King</string>
                </array>
            </dict>
        </dict>
    </array>
</dict>

因此,我需要帮助的是如何根据所需的 CategoryID 获取每个部分中的人员。我认为对我来说主要问题是,如何确定要从中提取信息的 CategoryID(即我知道 CategoryID,但如何将其与正确的部分相关联),然后如何循环遍历每个部分键(a 、b、c 等),当键是节的名称时(这有意义吗?)。

非常感谢任何帮助和想法!谢谢!

another fun (and probably really simple) question for you, that I have half worked out and now run into a dead end...

I need to build an indexed table using data from a plist which looks something like this:

<?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>Categories</key>
    <array>
        <dict>
            <key>CategoryName</key>
            <string>Test Category</string>
            <key>CategoryID</key>
            <integer>10</integer>
            <key>Sections</key>
            <dict>
                <key>A</key>
                <array>
                    <string>A Jones</string>
                    <string>A King</string>
                </array>
                <key>T</key>
                <array>
                    <string>T Jones</string>
                    <string>T King</string>
                </array>
            </dict>
        </dict>
        <dict>
            <key>CategoryName</key>
            <string>Another Test Category</string>
            <key>CategoryID</key>
            <integer>20</integer>
            <key>Sections</key>
            <dict>
                <key>P</key>
                <array>
                    <string>P Jones</string>
                    <string>P King</string>
                </array>
                <key>S</key>
                <array>
                    <string>S Jones</string>
                    <string>S King</string>
                </array>
            </dict>
        </dict>
    </array>
</dict>

So, what I need help with is how to get the people in each section depending on the required CategoryID. I think the main problem for me is, how do I determine which CategoryID to pull info out of (ie I know the CategoryID, but how do I relate this to the correct section) and then how do i loop through each section key (a, b, c etc..) when the key is the name of the section (does that make sense?).

Any help and thoughts are greatly appreciated! Thanks!

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

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

发布评论

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

评论(1

千仐 2024-11-26 06:42:28
NSDictionary *myDictionary = //load your dictionary from the file here
NSArray *categoryArray = [myDictionary objectForKey:@"Categories"];

NSDictionary *neededCategory;
for (NSDictionary *category in categoryArray) {
     NSNumber *categoryID = (NSNumber *)[category objectForKey @"CategoryID"];
     if ([categoryID intValue] == neededCategoryID) {
          neededCategory = category;
          break;
     }
}

//Sections
NSDictionary *sections = [neededCategory objectForKey:@"Sections"];
NSArray *allSectionKeys = [sections allKeys];

for (NSString *key in allSectionKeys) {
   NSArray *name = [sections objectForKey:key];
   //Do something with the name here
}
NSDictionary *myDictionary = //load your dictionary from the file here
NSArray *categoryArray = [myDictionary objectForKey:@"Categories"];

NSDictionary *neededCategory;
for (NSDictionary *category in categoryArray) {
     NSNumber *categoryID = (NSNumber *)[category objectForKey @"CategoryID"];
     if ([categoryID intValue] == neededCategoryID) {
          neededCategory = category;
          break;
     }
}

//Sections
NSDictionary *sections = [neededCategory objectForKey:@"Sections"];
NSArray *allSectionKeys = [sections allKeys];

for (NSString *key in allSectionKeys) {
   NSArray *name = [sections objectForKey:key];
   //Do something with the name here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文