2010-04-19 5 views
1

j'ai fait une plist qui ressemble à ce que:d'extraire des données de Plist à tableau et le dictionnaire

<?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> 

Maintenant, je veux extraire les données comme ça (tous les « A » pour « Nom » pour une section et tout le 'B' 'Nom' 'à l'autre):

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]; 

Ceci afin de les afficher dans TableViewed.

Je sais que le problème est ici:

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

Mais je ne peux pas comprendre comment le faire droit.

Merci.

Répondre

0

Vous disposez d'un plist contenant un tableau de tableaux de dictionnaires.

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

La clé (Profils) que vous demandez n'existe pas dans votre plist. Il ne sert à rien de construire un nouveau dictionnaire pour tenir le dictionnaire existant de votre plist.

Si vous avez un tableau de dictionnaires (ou toute valeur de clé de codage des objets conformes), vous pouvez extraire un tableau de « Nom » Attributs par:

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

Merci, Mais il ne devrait pas être quelque chose comme ça: \t NSDictionary * indexADict = [NSDictionary dictionaryWithObject: [(NSArray *) [participants objectAtIndex: 0] objectAtIndex: 0] forKey: @ "Name"]; à la fin je veux obtenir seulement les noms pour mon dictionnaire afin de les voir dans la table sectionnée comme ceci: NSDictionary * dictionary = [listOfPeople objectAtIndex: indexPath.section]; \t \t NSArray * array = [dictionnaire objetForKey: @ "Nom"]; \t \t NSString * cellValue = [array objectAtIndex: indexPath.row]; \t \t cell.primaryLabel.text = cellValue; – Boaz

+0

les styles d'édition ne fonctionne pas dans le commentaire ?? J'ai posté ci-dessous le même commentaire – Boaz

+0

Réponse éditée dans la réponse précédente. –

Questions connexes