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:
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:
Dann kommt als Output:
Nach überschreiben: (null)
Das heisst er hat keins von den beiden Dictionarys gespeichert! Aber warum?
iComputerfreak
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