目标C,使用plist的正确部分并循环遍历以获取数据 [英] Objective C, Using the correct part of a plist and looping through to grab data
问题描述
另一个有趣的(也许很简单)的问题,我已经解决了一半,现在陷入了死胡同...
another fun (and probably really simple) question for you, that I have half worked out and now run into a dead end...
我需要使用来自plist的数据构建索引表,
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>
因此,我需要帮助的是如何根据所需的CategoryID吸引每个部分的人员.我认为对我来说主要的问题是,如何确定要从中提取信息的CategoryID(即,我知道CategoryID,但是如何将其与正确的部分相关联),然后如何遍历每个部分的键( ,b,c等.)时,键是该部分的名称(有意义吗?).
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
}
这篇关于目标C,使用plist的正确部分并循环遍历以获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!