views:

59

answers:

1

Hi all,

I have written some code that pulls info from a plist file and does a couple of things then eventually pops this data into a table.

This all works fine, the problem is I wrote the code pulling the data from a plist file within the resources folder, but the actual file will be created via an action and stored in memory. I need some help on getting my code working when pulling it from the new location.

current working code:

- (void) loadData{
// Load items
NSString *error;
NSPropertyListFormat format;
NSString *path = [[NSBundle mainBundle] pathForResource:@"CalculatorData" ofType:@"plist"];
NSData *plistData = [NSData dataWithContentsOfFile:path];
NSArray *amountData = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];
NSDictionary *amountData = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];

if (amountData) {
    self.amounts = [[NSMutableArray alloc] initWithCapacity:[amountData count]];
    result = 0;
    for (NSDictionary *amountsDictionary in amountData) {

        [self.amounts addObject:amountsDictionary];

        currentNumber = [[amountsDictionary objectForKey:@"Value"] floatValue];

        if ([[amountsDictionary objectForKey:@"Type"] isEqualToString:@"Plus"]) {
            result = result + currentNumber;
        } else {
            result = result - currentNumber;
        }
    }
    //Set main amount
    NSString *msg = [NSString stringWithFormat:@"£%.2f",result];
    [lblAmount setText:msg];
} else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Why not keep track of your income and expensies? Add new items below." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
}
}

I think to pull the info from the new file location i need something similar to below, but can't seem to get the output the same as above:

//Get file location
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"saveBudget.plist"];

// read it back in with different dictionary variable
NSMutableDictionary *amountData = [NSMutableDictionary dictionaryWithContentsOfFile:path];
if(amountData==nil ){
    NSLog(@"failed to retrieve dictionary from disk");
} else {  
    NSLog(@"%@", amountData);
}

The code creating the plist file is (this also needs a little work, as its currently always overwriting what was done before, when I would like it to add a new array):

-(void) addData {

// create a dictionary to store a fruit's characteristics
NSMutableDictionary *items = [[NSMutableDictionary alloc] init];
[items setObject:@"10" forKey:@"Value"];
[items setObject:@"tester" forKey:@"Description"];
[items setObject:@"Plus" forKey:@"Type"];

// create a dictionary to store all fruits
NSMutableDictionary *plist = [[NSMutableDictionary alloc] init];
[plist setObject:items forKey:@"0"];

//Get file location
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"saveBudget.plist"];

// write plist to disk
[plist writeToFile:path atomically:YES];
}

This also needs a little work as I would like it to be:

    <?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"&gt;
<plist version="1.0">
<array>
    <dict>
        <key>Date</key>
        <string>27/9/10</string>
        <key>Value</key>
        <string>10.97</string>
        <key>Description</key>
        <string>PaperRound</string>
        <key>Type</key>
        <string>Plus</string>
    </dict>
    <dict>
        <key>Date</key>
        <string>27/9/10</string>
        <key>Value</key>
        <string>10.97</string>
        <key>Description</key>
        <string>PaperRound</string>
        <key>Type</key>
        <string>Plus</string>
    </dict>
    <dict>
        <key>Date</key>
        <string>27/9/10</string>
        <key>Value</key>
        <string>10.97</string>
        <key>Description</key>
        <string>PaperRound</string>
        <key>Type</key>
        <string>Plus</string>
    </dict>
    <dict>
        <key>Date</key>
        <string>27/9/10</string>
        <key>Value</key>
        <string>10.97</string>
        <key>Description</key>
        <string>PaperRound</string>
        <key>Type</key>
        <string>Plus</string>
    </dict>
</array>
</plist>

Sorry for the long post but hopefully should be everything that relates. Any help on this would be great.

+1  A: 

If you want the outermost element of the plist to be an array, then the following lines in the code you posted are creating an object of the wrong type:

// create a dictionary to store all fruits
NSMutableDictionary *plist = [[NSMutableDictionary alloc] init];
[plist setObject:items forKey:@"0"];

I would also beware of leaving stale comment statements in your code, as they can sometimes cause you to mislead yourself. Here's a suggested rewrite of your addData method:

-(void)addData {

    // Create a dictionary representing an item
    //
    NSDictionary *item = [NSDictionary dictionaryWithObjectsAndKeys:
                          @"10", @"Value",
                          @"tester", @"Description",
                          @"Plus", @"Type",
                          nil];

    // Create an array of items. This must be an instance of NSArray or
    // NSMutableArray if you want the corresponding element in your plist
    // to be an array.
    //
    NSArray *items = [NSArray arrayWithObjects: item, nil];

    // Get file location (this part is fine).
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"saveBudget.plist"];

    // Write array of items to filesystem
    [items writeToFile:path atomically:YES];
}

With apologies, I haven't tested the above code, but it should at least serve to illustrate the point.

jlehr
Hi Jlehr, thanks for this, simple things help so much! One last thing though, how can I add to the array, not replace? Currently when a new item is added it overwrites the current one?
jimbo
I have managed to work it out, by reading the data first then adding the new item (addObject) to that it works...
jimbo
Hi jimbo, sorry I didn't see this earlier. That's right, to add objects to the end of an array, you can use `-addObject:`; if you want to insert elsewhere in the array, use `-insertObject:atIndex:`.
jlehr