Reputation: 6190
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".
Upvotes: 23
Views: 28845
Reputation: 6114
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];
Upvotes: 24
Reputation: 7102
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]
Upvotes: 47