2012-04-05 18 views
0

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

+0

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. –

+0

Ich denke, der Speicherort, an dem Sie die Datei erstellen möchten, existiert nicht. – Leena

+0

@GrahamLee: Entschuldigung, mein Schlechter. Es war ein Fehler meinerseits. –

Antwort

0

Siehe den untenstehenden Code und es funktioniert perfekt von meiner Seite. Sie haben gerade Verzeichnis nicht genauen Dateinamen in der Methode filePathOfBookmarks erwähnt. Ich kann Daten in der Datei favourites.txt speichern.

Sie können auch unten Links für weitere Informationen über Datei Handeling in iOS siehe

1) file saving-and loading/using the document directory to store files

2) iPhone File System

- (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.txt"]; 
} 
0

Einige Ihrer Code macht keinen Sinn (zu mich).

macht dies nicht alles:

if (![fileManager fileExistsAtPath: [self filePathOfBookmarks]]) 
{ 
    [self filePathOfBookmarks]; 
} 

Dann init Sie Ihr Datenobjekt mit Inhalt Lesezeichen-Datei - an diesem Punkt ist es jedoch möglich, dass die Datei nicht existiert:

NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: [self filePathOfBookmarks]]; 

Sie können einfach die beiden oben genannten Codeblöcke auslassen, da Sie es noch einmal überprüfen danach:

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

Das macht Sinn. Aber wenn man die ersten beiden Code-Blöcke lassen, stellen Sie sicher, dass Sie das Datenobjekt deklarieren:

NSMutableData *data; 

Dann stellen Sie die firstone Objekt in den Data-Dictionary. Dies scheint in Ordnung (obwohl mit diesem Code die Daten aus einer vorhandenen Bookmark-Datei zuvor überhaupt nicht verwendet werden).

Schreiben Sie Ihre Daten Wörterbuch scheint auch in Ordnung:

[data writeToFile: [self filePathOfBookmarks] atomically:YES]; 

natürlich vorausgesetzt, Ihre Methode filePathOfBookmarks einen gültigen Dateinamen zurückgibt.

Die Methode gibt jedoch ein Verzeichnis, keine Datei zurück. Fügen Sie ein Suffix wie so:

return [docDirectory stringByAppendingPathComponent:@"favorites.plist"]; 

Überprüfen Sie den Dateipfad für die Gültigkeit

NSLog(@"%@",[self filePathOfBookmarks]); 

verwenden, bevor Sie eine Datei schreiben.

versuchen auch

NSLog(@"%@",data); 

, um zu sehen, was Ihr Wörterbuch enthält, bevor es zu schreiben.

Wenn es immer noch nicht funktioniert, repost.

Verwandte Themen