Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete file obj c

I am creating files with the following code

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

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

To delete files I use the following

NSFileManager *manager = [NSFileManager defaultManager];

NSError *error = nil;

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

But neither the first nor the second path seem to work, I always get the error "no such file or directory".

like image 711
Marc Avatar asked Mar 19 '13 16:03

Marc


2 Answers

You used NSHomeDirectory() stringByAppendingPathComponent in the file creation, but not in either path when you try to delete the file. Try:

[manager removeItemAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents/xyz123.data"] error:&error]
like image 120
Aaron Golden Avatar answered Nov 09 '22 04:11

Aaron Golden


Try this:

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];
like image 40
Sergey Kuryanov Avatar answered Nov 09 '22 05:11

Sergey Kuryanov