2010-08-25 11 views
1

J'ai un peu de mal à enregistrer dans un fichier plist, quand je lis les données que je suis en utilisant:Enregistrement des données dans un fichier plist

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return amounts.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    //Create Cell 
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"]; 

    //Fill cell 
    NSDictionary *budgetItem = [amounts objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [budgetItem objectForKey:@"value"]; 
    cell.detailTextLabel.text = [budgetItem objectForKey:@"description"]; 

    //Return it 
    return cell; 
} 

- (void) loadData{ 

    // Load items 
    NSString *error; 
    NSPropertyListFormat format; 
    NSString *localizedPath = [[NSBundle mainBundle] pathForResource:@"savebudget" ofType:@"plist"]; 
    NSData *plistData = [NSData dataWithContentsOfFile:localizedPath]; 

    NSArray *amountData = [NSPropertyListSerialization propertyListFromData:plistData 
                 mutabilityOption:NSPropertyListImmutable 
                    format:&format 
                 errorDescription:&error]; 
    if (amountData) { 
     self.amounts = [[NSMutableArray alloc] initWithCapacity:[amountData count]]; 
     for (NSDictionary *amountsDictionary in amountData) { 
      [self.amounts addObject:amountsDictionary]; 
    } 
} 

Ce qui fonctionne très bien à partir d'un fichier plist statique dans mon dossier de ressources, mais quand je tente de créer mon propre, rien ne semble se passer:

-(void) addData { 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"saveBudget" ofType:@"plist"]; 
    NSMutableDictionary* plist = [[NSMutableDictionary alloc] initWithContentsOfFile:path]; 
    [plist setValue:amountTxt.text forKey:@"value"]; 
    [plist writeToFile:path atomically:YES]; 
    [plist release]; 

} 

- (IBAction)add:(id)sender { 
    [self addData]; 
    [self.delegate budgetEnterMinusViewControllerDidFinish:self]; 
} 

Toute aide plus que bienvenue ...

Répondre

2

Ugh. Code terrible et déroutant. Tout d'abord: utiliser cette option pour charger la place:

NSString* path = [[NSBundle mainBundle] pathForResource:@"savebudget" ofType:@"plist"]; 

NSDictionary* amountData = [NSDictionary dictionaryWithContentsOfFile: path error: NULL]; 
if (amountData) { 
    self.amounts = [NSMutableArray arrayWithArray: amountData]; 
} 

Remarque, pas conserver ou alloc/init ici parce que vous assignez à une propriété de retenue.

Donc le vrai problème:

Vous lisez un plist que vous dites contient un tableau de dictionnaires. Mais lorsque vous ajoutez des données, vous essayez de réécrire un seul dictionnaire dans la même liste.

De même, dans votre méthode addData, vous n'ajoutez aucune donnée. Et ... Si vous chargez vos données initiales à partir de l'ensemble de votre application, vous devez les réécrire dans votre répertoire ~/Documents après l'avoir modifié. Et bien sûr, lisez-le de nouveau la prochaine fois que votre application démarre.

+0

Salut St3fan, comment puis-je le modifier pour écrire correctement? Impossible de trouver des informations à ce sujet, seulement pour écrire dans un seul tableau/dictionnaire ... – jimbo

Questions connexes