NSDictionary kann nicht gespeichert werden

I

iComputerfreak

Aktives Mitglied
Thread Starter
Dabei seit
31.05.2013
Beiträge
129
Reaktionspunkte
1
Hi,
ich habe das Problem, dass ich ein Dictionary in objective-c nicht speichern kann.
Hier erst mal mein Code:

Code:
[COLOR=#FFFFFF][FONT=Menlo]- ([COLOR=#de38a6]IBAction[/COLOR])saveChanges:([COLOR=#de38a6]id[/COLOR])sender {[/FONT][/COLOR]
[COLOR=#00B1FF][FONT=Menlo]NSDictionary[COLOR=#ffffff] *preferences = [[/COLOR]NSDictionarydictionaryWithObjectsAndKeys[COLOR=#ffffff]:[/COLOR][/FONT][/COLOR]
[COLOR=#FFFFFF][FONT=Menlo]                                [COLOR=#08fa95]picsPerGameAnzeige[/COLOR].[COLOR=#00b1ff]text[/COLOR], [COLOR=#ff4647]@"picsPerGame"[/COLOR],[/FONT][/COLOR]
[COLOR=#FFFFFF][FONT=Menlo]                                [COLOR=#de38a6]nil[/COLOR]];[/FONT][/COLOR]
[COLOR=#FF4647][FONT=Menlo][COLOR=#00b1ff]NSLog[/COLOR][COLOR=#ffffff]([/COLOR]@"Nach Erstellung: '%@'"[COLOR=#ffffff], [preferences [/COLOR][COLOR=#00b1ff]objectForKey[/COLOR][COLOR=#ffffff]:[/COLOR]@"picsPerGame"[COLOR=#ffffff]]);[/COLOR][/FONT][/COLOR]
[COLOR=#00B1FF][FONT=Menlo][COLOR=#ffffff]   [preferences [/COLOR]writeToFile[COLOR=#ffffff]:[[[/COLOR]NSBundlemainBundle[COLOR=#ffffff]] [/COLOR]pathForResource[COLOR=#ffffff]:[/COLOR][COLOR=#ff4647]@"preferences"[/COLOR]ofType[COLOR=#ffffff]:[/COLOR][COLOR=#ff4647]@"xml"[/COLOR][COLOR=#ffffff]] [/COLOR]atomically[COLOR=#ffffff]:[/COLOR][COLOR=#de38a6]YES[/COLOR][COLOR=#ffffff]];[/COLOR][/FONT][/COLOR]

[COLOR=#00B1FF][FONT=Menlo]NSDictionary[COLOR=#ffffff] *read = [[/COLOR]NSDictionarydictionaryWithContentsOfFile[COLOR=#ffffff]:[[[/COLOR]NSBundlemainBundle[COLOR=#ffffff]] [/COLOR]pathForResource[COLOR=#ffffff]:[/COLOR][COLOR=#ff4647]@"preferences"[/COLOR]ofType[COLOR=#ffffff]:[/COLOR][COLOR=#ff4647]@"xml"[/COLOR][COLOR=#ffffff]]];[/COLOR][/FONT][/COLOR]
[COLOR=#FF4647][FONT=Menlo][COLOR=#00b1ff]NSLog[/COLOR][COLOR=#ffffff]([/COLOR]@"Gespeichert und wieder geladen: '%@'"[COLOR=#ffffff], [read [/COLOR][COLOR=#00b1ff]objectForKey[/COLOR][COLOR=#ffffff]:[/COLOR]@"picsPerGame"[COLOR=#ffffff]]);[/COLOR][/FONT][/COLOR]
[COLOR=#FFFFFF][FONT=Menlo]}[/FONT][/COLOR]

Im Output kommt dann:

Nach Erstellung: '22'
Gespeichert und wieder geladen: '10'

Nach dem erneuten laden ist der Wert 10. 10 ist der Standardwert. Ich habe nämlich in Xcode eine xml-Datei mit dem Namen 'preferences' schon erstellt, in der ein Dictionary mit dem Wert 10 gespeichert ist. Ich kann dies aber nicht überschreiben, wie man an dem Beispiel sieht. Oder muss ich die alte xml-Datei erst löschen? oder gibt es eine Methode, mit der es die alte überschreibt?

Wenn ich diesen Code hier ausführe:

Code:
[COLOR=#00B1FF][FONT=Menlo]NSDictionary[COLOR=#ffffff] *testDict1 = [[/COLOR]NSDictionarydictionaryWithObjectsAndKeys[COLOR=#ffffff]:[/COLOR][COLOR=#ff4647]@"999"[/COLOR][COLOR=#ffffff], [/COLOR][COLOR=#ff4647]@"pics"[/COLOR][COLOR=#ffffff], [/COLOR][COLOR=#de38a6]nil[/COLOR][COLOR=#ffffff]];[/COLOR][/FONT][/COLOR]
[COLOR=#00B1FF][FONT=Menlo][COLOR=#ffffff]   [testDict1 [/COLOR]writeToFile[COLOR=#ffffff]:[[[/COLOR]NSBundlemainBundle[COLOR=#ffffff]] [/COLOR]pathForResource[COLOR=#ffffff]:[/COLOR][COLOR=#ff4647]@"testDict"[/COLOR]ofType[COLOR=#ffffff]:[/COLOR][COLOR=#ff4647]@"xml"[/COLOR][COLOR=#ffffff]] [/COLOR]atomically[COLOR=#ffffff]:[/COLOR][COLOR=#de38a6]YES[/COLOR][COLOR=#ffffff]];[/COLOR][/FONT][/COLOR]

[COLOR=#00B1FF][FONT=Menlo]NSDictionary[COLOR=#ffffff] *testDict2 = [[/COLOR]NSDictionarydictionaryWithObjectsAndKeys[COLOR=#ffffff]:[/COLOR][COLOR=#ff4647]@"888"[/COLOR][COLOR=#ffffff], [/COLOR][COLOR=#ff4647]@"pics"[/COLOR][COLOR=#ffffff], [/COLOR][COLOR=#de38a6]nil[/COLOR][COLOR=#ffffff]];[/COLOR][/FONT][/COLOR]
[COLOR=#00B1FF][FONT=Menlo][COLOR=#ffffff]   [testDict2 [/COLOR]writeToFile[COLOR=#ffffff]:[[[/COLOR]NSBundlemainBundle[COLOR=#ffffff]] [/COLOR]pathForResource[COLOR=#ffffff]:[/COLOR][COLOR=#ff4647]@"testDict"[/COLOR]ofType[COLOR=#ffffff]:[/COLOR][COLOR=#ff4647]@"xml"[/COLOR][COLOR=#ffffff]] [/COLOR]atomically[COLOR=#ffffff]:[/COLOR][COLOR=#de38a6]YES[/COLOR][COLOR=#ffffff]];[/COLOR][/FONT][/COLOR]

[COLOR=#00B1FF][FONT=Menlo]NSDictionary[COLOR=#ffffff] *readDict = [[/COLOR]NSDictionarydictionaryWithContentsOfFile[COLOR=#ffffff]:[[[/COLOR]NSBundlemainBundle[COLOR=#ffffff]] [/COLOR]pathForResource[COLOR=#ffffff]:[/COLOR][COLOR=#ff4647]@"testDict"[/COLOR]ofType[COLOR=#ffffff]:[/COLOR][COLOR=#ff4647]@"xml"[/COLOR][COLOR=#ffffff]]];[/COLOR][/FONT][/COLOR]

[COLOR=#FF4647][FONT=Menlo][COLOR=#00b1ff]NSLog[/COLOR][COLOR=#ffffff]([/COLOR]@"Nach überschreiben: %@"[COLOR=#ffffff], [readDict [/COLOR][COLOR=#00b1ff]objectForKey[/COLOR][COLOR=#ffffff]:[/COLOR]@"pics"[COLOR=#ffffff]]);[/COLOR][/FONT][/COLOR]

Dann kommt als Output:

Nach überschreiben: (null)

Das heisst er hat keins von den beiden Dictionarys gespeichert! Aber warum?

iComputerfreak
 
Du versuchst direkt ins mainBundle zu schreiben und dazu hat deine App keine Rechte. Das hat den Grund, dass alles im mainBundle kryptographisch signiert wird. Du kannst zwar unter Umständen den Simulator dazu bringen ins mainBundle zu schreiben, weil der auch unsignierten Code ausführt (deswegen braucht man auch kein Developer Program um den zu benutzen), aber auf einem echten Device wird der Versuch scheitern.
Du musst zunächst mal einen gültigen Pfad bekommen. In dem Fall hier könntest du deine Datei in den Documents Ordner legen.
Implementiere z.B. eine Methode, die wie folgt aussieht:

Code:
- (NSString*)fileSavingPath
{
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [[path objectAtIndex:0] stringByAppendingPathComponent:@"savegame.plist"];
}

'savegame.plist' ersetzt du dabei durch irgendeinen Dateinamen/-typen deiner Wahl.

Danach kannst du innerhalb deines Codes wie folgt in Dateien schreiben:

Code:
//File Handle bekommen
NSFileHandle *file = [NSFileHandle fileHandleForWritingAtPath:[self fileSavingPath]];
//Existiert die Datei bereits? Wenn nein, leg sie an.
if (file == nil) {
    [[NSFileManager defaultManager] createFileAtPath:[self fileSavingPath]
                                            contents:nil
                                            attributes:nil];
    file = [NSFileHandle fileHandleForWritingAtPath:[self fileSavingPath]];
}
        
//Schreiben und danach das File wieder zu machen
[file writeData:object]; //Für 'object' fügst du das Objekt ein, dass du schreiben willst
[file closeFile];
 
Vielen Dank! Hat mir sehr geholfen. Ich benutze aber dennoch weiterhin die methode [NSDictionary writeToFile].
Aber ich ersetze halt den mainBundle Pfad durch deinen Pfad.
 
Zurück
Oben Unten