web-dev-qa-db-fra.com

Supprimer le fichier obj c

Je crée des fichiers avec le code suivant

NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *filename = @"xyz123.data";
docPath = [NSString stringWithFormat:@"%@/%@", docPath, filename];

NSError *error = nil;
[data writeToFile:docPath options:0 error:&error];

Pour supprimer des fichiers, j'utilise ce qui suit

NSFileManager *manager = [NSFileManager defaultManager];

NSError *error = nil;

NSString *path = @"xyz123.data";
//NSString *path = @"Documents/xyz123.data";
[manager path error:&error];

Mais ni le premier ni le deuxième chemin ne semblent fonctionner, je reçois toujours l'erreur "Aucun fichier ou répertoire de ce type".

22
Marc

Vous avez utilisé NSHomeDirectory() stringByAppendingPathComponent Dans la création de fichier, mais pas dans l'une ou l'autre chemin lorsque vous essayez de supprimer le fichier. Essayer:

[manager removeItemAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/xyz123.data"] error:&error]
43
Aaron Golden

Essaye ça:

NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [docPath stringByAppendingPathComponent:@"xyz123.data"];
NSError *error = nil;
[data writeToFile:filePath options:0 error:&error];
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
23
Sergey Kuryanov