删除文件 obj c [英] Delete file obj c
本文介绍了删除文件 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".
推荐答案
您在文件创建中使用了 NSHomeDirectory() stringByAppendingPathComponent
,但在尝试删除文件时不在任一路径中使用.试试:
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]
这篇关于删除文件 obj c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文