Ich habe diesen Code. Dieser Code erstellt keine Datei im Document-Verzeichnis und ich habe keine Ahnung warum. Kann jemand sehen, was ich vermisse? "data" dictionary hat, was ich brauche, aber wenn es um writeToFile geht: Es passiert nicht, weil keine Datei erstellt wurde. Der gleiche Code funktioniert in anderen Apps, die ich habe, ich vermisse sicherlich etwas.NSFileManager Kann keine Datei erstellen
- (void)addBookmark{
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: [self filePathOfBookmarks]])
{
[self filePathOfBookmarks];
}
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: [self filePathOfBookmarks]];
if ([fileManager fileExistsAtPath: [self filePathOfBookmarks]])
{
data = [[NSMutableDictionary alloc] initWithContentsOfFile: [self filePathOfBookmarks]];
}
else
{
// If the file doesn’t exist, create an empty dictionary
data = [[NSMutableDictionary alloc] init];
}
NSMutableDictionary *firstOne = [NSMutableDictionary dictionary];
[firstOne setObject:@"one" forKey:@"name"];
[firstOne setObject:@"two" forKey:@"call"];
[firstOne setObject:@"twoandhalf" forKey:@"email"];
[firstOne setObject:@"four" forKey:@"description"];
[data setObject:firstOne forKey:@"ID"];
[data writeToFile: [self filePathOfBookmarks] atomically:YES];
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: [self filePathOfBookmarks]];
NSLog(@"file content %@",savedStock);
}
- (NSString *) filePathOfBookmarks {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
return [docDirectory stringByAppendingPathComponent:@"favourites"];
}
NSLog(@"%@",[self filePathOfBookmarks]);
dies zurück:
/Users/Turm/Library/Application Support/iPhone Simulator/5.1/Anwendungen/40CAA37F-6477-4101-A142-D4797748ABD9/Dokumente/Favoriten
Was bedeutet 'filePathOfBookmarks'? Gibt es den richtigen Pfad zurück? Ich muss Sie einmal überprüfen und wenn möglich unter Ihre Frage einfügen. –
Ich denke, der Speicherort, an dem Sie die Datei erstellen möchten, existiert nicht. – Leena
@GrahamLee: Entschuldigung, mein Schlechter. Es war ein Fehler meinerseits. –