删除文件OBJÇ(Delete file obj c)

2019-08-17 17:56发布

我创建用下面的代码文件

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

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

要删除的文件我用下面的

NSFileManager *manager = [NSFileManager defaultManager];

NSError *error = nil;

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

但是,无论是第一还是第二路径似乎工作,我总是得到错误“没有这样的文件或目录”。

Answer 1:

您使用NSHomeDirectory() stringByAppendingPathComponent在文件的创建,而不是在任一路径,当您尝试删除该文件。 尝试:

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


Answer 2:

试试这个:

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


文章来源: Delete file obj c