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">
<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.