23

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".

2
  • show declaration of data Mar 19, 2013 at 16:49
  • NSData *fullData = [NSData dataWithContentsOfURL:tuple[1]]; It is then passed to my create method
    – Marc
    Mar 19, 2013 at 16:51

2 Answers 2

46

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]
0
24

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];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.