- Registriert
- 18.08.08
- Beiträge
- 129
Hallo liebe Apfeltalker,
ich habe ein echt nerviges Problem mit meinem Programm:
In der Funktion awakeonNib suche ich nach der Datei Contacts.plist, falls sie vorhanden ist lese ich sie ein, ansonsten erstelle ich sie neu.
Das klappt soweit auch. Das Problem ist jetzt, egal wie oft awakeonNib aufgerufen wird, jedesmal wird die Datei neu erstellt, die zuvor erstellte Datei wird einfach nicht erkannt.
Wenn ich hingegen das Programm schliesse und neu öffne, erkennt er die Datei richtig, daraus lesen sowie speichern ist möglich, woraus ich schliesse, das meine Funktionen prinzipiell in Ordnung sind und der Fehler in der Bedingung [fileManager fileExistsAtPath:filePath] zu suchen ist. Leider komme ich nicht so recht auf den Fehler, deshalb bräuchte ich mal die Hilfe der Profis
Anhand des Textfeldes txtstatus bin ich darauf gekommen, das die if-Bedingung immer FALSE ist, auch wenn die File schon angelegt wurde. Nach dem Neustart ist aber alles i.O, File wird erkannt und alles passt.
Muss ich irgendwie die File "nachladen", um sie während der Laufzeit verfügbar zu machen?
Bin für jeden Hinweis dankbar!
Grüße Madhatter//Ralf
EDIT: Code auch zu finden unter:
http://pastebin.com/tjNYR7Dp
Ist da vll. etwas übersichtlicher
ich habe ein echt nerviges Problem mit meinem Programm:
In der Funktion awakeonNib suche ich nach der Datei Contacts.plist, falls sie vorhanden ist lese ich sie ein, ansonsten erstelle ich sie neu.
Das klappt soweit auch. Das Problem ist jetzt, egal wie oft awakeonNib aufgerufen wird, jedesmal wird die Datei neu erstellt, die zuvor erstellte Datei wird einfach nicht erkannt.
Wenn ich hingegen das Programm schliesse und neu öffne, erkennt er die Datei richtig, daraus lesen sowie speichern ist möglich, woraus ich schliesse, das meine Funktionen prinzipiell in Ordnung sind und der Fehler in der Bedingung [fileManager fileExistsAtPath:filePath] zu suchen ist. Leider komme ich nicht so recht auf den Fehler, deshalb bräuchte ich mal die Hilfe der Profis

Code:
NSString *filePath;
NSString *samplePath;
- (void)update_plist {
//save the changes to the plist
[[arrayContacts arrangedObjects] writeToFile:filePath atomically:YES];
}
- (void)awakeFromNib {
int i = 0;
NSMutableDictionary *dict;
NSFileManager *fileManager = [NSFileManager alloc];
//set up the path of the plist(which will reside in the Resource folder)
filePath = [[NSBundle mainBundle] pathForResource:@"Contacts" ofType:@"plist"];
samplePath = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"plist"];
//initialize the contacts array
contacts = [[NSMutableArray alloc] init];
dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"1",@"cust_id",
@"-", @"firstName",
@"-", @"lastName",
@"-", @"info",
nil];
//check if the file Contacts.plist exists -------------- hier vermute ich den Fehler!
if ([fileManager fileExistsAtPath:filePath]) { //--> Auch schon probiert: if(filePath != nil)...
//if it does read its contents into an array and add the contacts to the arrayController wich will then feed the table
NSMutableArray *contactsFile = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
for (id contact in contactsFile) {
[arrayContacts addObject:contact];
i++;
}
if (i == 0) {
[arrayContacts addObject:dict];
}
[txtstatus setStringValue:@"File exists"];
}
else {
//File Contacts.plist erstellen
[fileManager copyItemAtPath:@"adressbook.app/Contents/Resources/Sample.plist" toPath: @"adressbook.app/Contents/Resources/Contacts.plist" error:nil];
//add it to the arrayController
[arrayContacts addObject:dict];
[txtstatus setStringValue:@"File created"];
}
[self update_plist];
}
Muss ich irgendwie die File "nachladen", um sie während der Laufzeit verfügbar zu machen?
Bin für jeden Hinweis dankbar!
Grüße Madhatter//Ralf
EDIT: Code auch zu finden unter:
http://pastebin.com/tjNYR7Dp
Ist da vll. etwas übersichtlicher
