Question

Apparemment, NSFileManager ne peut pas supprimer les fichiers créés par mkstemp(). Voici un code de test pour démontrer:

char pathCString[] = "/tmp/temp.XXXXXX";
int fileDescriptor = mkstemp(pathCString);
if (fileDescriptor == -1) {
    NSLog(@"mkstemp failed");
} else {
    close(fileDescriptor);
    NSURL *url = [NSURL URLWithString:[NSString stringWithCString:pathCString encoding:NSASCIIStringEncoding]];
    NSLog(@"URL: %@", url);
    NSError *error;
    if (![[NSFileManager defaultManager] removeItemAtURL:url error:&error]) {
        NSLog(@"could not delete file: %@", error);
    }
}

Voici ce que je vois dans le journal quand je lance le code ci-dessus:

URL: /tmp/temp.A7DsLW
could not delete file: Error Domain=NSCocoaErrorDomain Code=4 UserInfo=0x1001108a0 "The file “temp.A7DsLW” doesn’t exist."

Je suis en cours d'exécution sur ce Snow Leopard. Toutes les idées sur la raison pour laquelle le problème se produit et / ou comment travailler autour d'elle?

Merci!

Était-ce utile?

La solution

Ne pas utiliser -URLWithString :, utilisation -fileURLWithPath: vous n'avez pas une URL de fichier valide. En passant la chaîne de chemin directement au -removeItemAtPath de NSFileManager. Sera bien sûr plus court

En outre, pour les chemins de fichiers, assurez-vous toujours la chaîne de chemin avec -stringWithUTF8String:.

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