从Plist中提取数据到数组和字典

时间:2010-04-19 15:00:38

标签: xml iphone-sdk-3.0 nsarray plist nsdictionary

我做了一个看起来那样的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">
<array>
<array>
    <dict>
        <key>Company</key>
        <string>xxx</string>
        <key>Title</key>
        <string>VP Marketing</string>
        <key>Name</key>
        <string>Alon ddfr</string>
    </dict>
    <dict>
        <key>Name</key>
        <string>Adam Ben Shushan</string>
        <key>Title</key>
        <string>CEO</string>
        <key>Company</key>
        <string>Shushan ltd.</string>
    </dict>
</array>
<array>
    <dict>
        <key>Company</key>
        <string>xxx</string>
        <key>Title</key>
        <string>CTO</string>
        <key>Name</key>
        <string>Boaz frf</string>
    </dict>
</array>
</array>
</plist>

现在我想提取这样的数据(所有'A'代表键“Name”到一个部分,所有'B'“Name”到另一个部分):

NSString *plistpath = [[NSBundle mainBundle] pathForResource:@"PeopleData" ofType:@"plist"];
NSMutableArray *attendees = [[NSMutableArray alloc] initWithContentsOfFile:plistpath];


listOfPeople = [[NSMutableArray alloc] init];//Add items

NSDictionary *indexADict = [NSDictionary dictionaryWithObject:[[attendees objectAtIndex:0] objectForKey:@"Name"] forKey:@"Profiles"];


NSDictionary *indexBDict = [NSDictionary dictionaryWithObject:[[attendees objectAtIndex:1] objectForKey:@"Name"]  forKey:@"Profiles"];

[listOfPeople addObject:indexADict];
[listOfPeople addObject:indexBDict];

这是为了在分段的tableView中查看它们。

我知道问题出在这里:

NSDictionary *indexADict = [NSDictionary dictionaryWithObject:[[attendees objectAtIndex:0] objectForKey:@"Name"] forKey:@"Profiles"];

但我无法想象如何正确行事。

感谢。

1 个答案:

答案 0 :(得分:0)

你有一个包含字典数组数组的plist。

NSDictionary *indexADict =  (NSDictionary *)[(NSArray *)[attendees objectAtIndex:0] objectAtIndex:0];

您要求的密钥(个人资料)在您的plist中不存在。构建一个新的字典来保存你的plist中的现有字典毫无意义。

如果您有一个字典数组(或任何键值编码兼容对象),您可以通过以下方式提取“名称”属性数组:

NSArray *names = [array valueForKey:@"Name"];