writeToFile ist nicht auf die Datei schreiben

Nun, ich bin ein problem, und ich habe gekämpft, mit ihm für ein paar Tage. Hier ist es: wenn man versucht zu schreiben, um eine xml-Datei (befindet sich in der xcode-Projekt, für welches ich entwickle) mit writeToFile, schreiben funktioniert nicht und ich kann nichts sehen in der xml-Datei, obwohl der bool-Wert, der zurückgegeben wird, von writeToFile wird ausgewertet zu true !! Neben der Datei-bytes null sind. Also, ich wäre wirklich dankbar, wenn jemand kann mir helfen mit, dass. Unten ist Teil der code, den ich schrieb,:

//writing to a file
 NSString *pathOfFile = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"xml"];
 NSString *dummyString = @"Hello World!!\n"; 
 NSFileManager *filemanager;

 filemanager = [NSFileManager defaultManager];

  //entering the first condition so I assured that the file do exists
  if ([filemanager fileExistsAtPath:pathOfFile] == YES)
     NSLog (@"File do exist !!!");
   else
     NSLog (@"File not found !!!");


BOOL writingStatus = [dummyString writeToFile:path atomically:YES encoding:NSUnicodeStringEncoding error:nil];

    //Not entering this condition so I am assuming that writing process is successful but when I open the file I can't find the string hello world and file size shows 0 bytes
    if(!writingStatus)
    {
        NSLog(@"Error: Could not write to the file");
    }

Ich habe auch versucht, diese alternative, aber leider hat es nicht funktioniert auch.

NSString *hello_world = @"Hello World!!\n";
NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *directory = [paths objectAtIndex:0];
NSString *fileName = @"sample.xml";
NSString *filePath = [directory stringByAppendingPathComponent:fileName];
NSLog(@"%@",filePath);
BOOL sucess = [hello_world writeToFile:filePath atomically:YES encoding:NSASCIIStringEncoding error:nil];  
if(!sucess)
{
    NSLog(@"Could not to write to the file");
}
Reden wir über iOS-oder MacOS-X hier? Kennzeichnen Sie bitte Ihre Frage zu geben.

InformationsquelleAutor User897 | 2011-10-03

Schreibe einen Kommentar