views:

74

answers:

1

Hi All,

I am having a little trouble saving to a plist file, when i am reading the data i am using:

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

Which works fine from a static plist file with-in my resources folder, but when i try and create my own, nothing seems to happen:

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

Any help more than welcome...

+2  A: 

Ugh. Terrible and confusing code. First: use this to load instead:

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

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

Note, no retain or alloc/init here because you are assigning to a retaining property.

So the real problem:

You are reading a plist that that you say contains an array of dictionaries. But then when you add data, you try to write back one single dictionary to that same plist.

Also, in your addData method you do not actually add any data.

And ... If you load your initial data from your app's bundle, then you should write it back to your ~/Documents directory after changing it. And of course read it back from there the next time your app starts.

St3fan
Hi St3fan, how can i change it to write correctly? Can't find any info on this, only to write back to a single array/dictionary...
jimbo