Wörterbuch-und plist-Dateien

Ich geschrieben habe einige code, der zieht info aus einer plist-Datei und ein paar Sachen dann letztendlich erscheint diese Daten in eine Tabelle.

Diesem alles Prima funktioniert, das problem ist, ich schrieb den code ziehen der Daten aus einer plist-Datei in den resources-Ordner, aber die eigentliche Datei wird erstellt, über eine Aktion und im Speicher abgelegt. Ich brauche etwas Hilfe zu bekommen mein code funktioniert, wenn Sie ihn von der neuen Lage.

aktuelle funktionierende 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];
}
}

Denke ich und ziehe die Infos aus dem neuen Speicherort der Datei, die ich brauche sowas ähnliches wie unten, aber kann nicht scheinen, um die Ausgabe die gleiche wie oben:

//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);
}

Den code Erstellung der plist-Datei ist (dies muss auch ein wenig Arbeit, da es derzeit immer überschreiben, was zuvor getan wurde, wenn ich es möchte hinzufügen eines neuen 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];
}

Dies muss auch ein wenig Arbeit, wie ich es gerne hätte:

    <?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 für den langen post sollte aber hoffentlich alles, was sich bezieht.
Jede Hilfe zu diesem wäre toll.

InformationsquelleAutor jimbo | 2010-09-14

Schreibe einen Kommentar