开发者

Objective C, Using the correct part of a plist and looping through to grab data

开发者 https://www.devze.com 2023-03-18 02:56 出处:网络
another fun (and probably really simple) question for you, that I have half worked out and now run into a dead end...

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>开发者_JS百科;
                    <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!


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消