writeToFile n'est pas écrit dans le fichier

Eh bien, je suis confronté à un problème et j'ai eu du mal avec elle pour quelques jours. Ici, c'est: quand j'essaie d'écrire dans un fichier xml (placé dans le projet xcode qui je suis en développement) à l'aide de writeToFile, l'écriture ne fonctionne pas et je ne vois rien dans le fichier xml bien que la valeur booléenne qui est retourné à partir de writeToFile est évalué à true !! En outre, le fichier d'octets à zéro. Donc, je voudrais vraiment l'apprécier si quelqu'un peut m'aider avec ça. Ci-dessous est une partie du code que j'ai écrit:

//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");
    }

J'ai aussi essayé cette solution, mais malheureusement, il ne fonctionne pas trop.

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");
}
Nous parlons de iOS ou mac os X ici? Veuillez marquer votre question pour indiquer que.

OriginalL'auteur User897 | 2011-10-03