Question

mon application iPad a une petite installation de téléchargement, pour lequel je veux ajouter les données au moyen d'un NSFileHandle. Le problème est l'appel de création renvoie uniquement les descripteurs de fichiers nuls. Quel pourrait être le problème? Voici les trois lignes de code qui sont censés créer mon descripteur de fichier:

NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
self.finalPath = [applicationDocumentsDirectory stringByAppendingPathComponent: self.fileName]; 
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];

J'ai vérifié le chemin du fichier, et je ne pouvais rien voir de mal.

TYIA

Était-ce utile?

La solution

fileHandleForWritingAtPath est pas un appel « de création ». La documentation indique explicitement: « Valeur de retour: Le descripteur de fichier initialisées, ou nil si aucun fichier existe au chemin » (nous soulignons). Si vous souhaitez créer le fichier si elle le fait, vous auriez existe de ne pas utiliser quelque chose comme ceci:

 NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
 if(output == nil) {
      [[NSFileManager defaultManager] createFileAtPath:self.finalPath contents:nil attributes:nil];
      output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
 }

Si vous voulez ajouter au fichier si elle existe déjà, utilisez quelque chose comme [output seekToEndOfFile]. Votre code complet serait alors se présenter comme suit:

 NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
 self.finalPath = [applicationDocumentsDirectory stringByAppendingPathComponent: self.fileName]; 
 NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
 if(output == nil) {
      [[NSFileManager defaultManager] createFileAtPath:self.finalPath contents:nil attributes:nil];
      output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
 } else {
      [output seekToEndOfFile];
 }

Autres conseils

Obtenir le chemin du répertoire des documents

+(NSURL *)getDocumentsDirectoryPath
{
    return [[[NSFileManager defaultManager]URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]lastObject];
}

Enregistrer le texte à la fin du fichier

si le fichier ne marche pas exist créer et données d'écriture

+(void)saveText:(NSString *)textTobeSaved atPath:(NSString*)fileName
{
    NSString *filePath = [NSString stringWithFormat:@"%@.text",fileName];

    NSString *path = [[self getDocumentsDirectoryPath].path
                      stringByAppendingPathComponent:filePath];
    NSFileHandle *fileHandler = [NSFileHandle fileHandleForWritingAtPath:path];
    if(fileHandler == nil) {
        [[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
        fileHandler = [NSFileHandle fileHandleForWritingAtPath:path];
    } else {
        textTobeSaved = [NSString stringWithFormat:@"\n-----------------------\n %@",textTobeSaved];
        [fileHandler seekToEndOfFile];
    }

    [fileHandler writeData:[textTobeSaved dataUsingEncoding:NSUTF8StringEncoding]];
    [fileHandler closeFile];
}

obtenir le texte du fichier avec le nom de fichier spécifié

+(NSString *)getTextFromFilePath:(NSString *)fileName
{
    NSString *filePath = [NSString stringWithFormat:@"%@.text",fileName];

    NSString *path = [[self getDocumentsDirectoryPath].path
                      stringByAppendingPathComponent:filePath];
    NSLog(@"%@",path);
    if(path!=nil)
    {
    NSString *savedString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

    return savedString;
  }else{
    return @"";
  } 
}

Supprimer le fichier

+(void)deleteFile:(NSString *)fileName
{
    NSString *filePath = [NSString stringWithFormat:@"%@.text",fileName];

    NSString *path = [[self getDocumentsDirectoryPath].path
                      stringByAppendingPathComponent:filePath];

    NSFileHandle *fileHandler = [NSFileHandle fileHandleForWritingAtPath:path];
    if(fileHandler != nil) {
        [[NSFileManager defaultManager]removeItemAtPath:path error:nil];
    }

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top