【发布时间】:2013-02-07 16:48:28
【问题描述】:
我想从我的应用文档目录中删除一张图片。我为删除图像而编写的代码是:
-(void)removeImage:(NSString *)fileName
{
fileManager = [NSFileManager defaultManager];
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsPath = [paths objectAtIndex:0];
filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", fileName]];
[fileManager removeItemAtPath:filePath error:NULL];
UIAlertView *removeSuccessFulAlert=[[UIAlertView alloc]initWithTitle:@"Congratulation:" message:@"Successfully removed" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
[removeSuccessFulAlert show];
}
它的部分工作。此代码从目录中删除文件,但是当我检查目录中的内容时,它仍然在那里显示图像名称。我想从目录中完全删除该文件。我应该在代码中更改什么来做同样的事情?谢谢
【问题讨论】:
-
可能会抛出你忽略的错误,添加 NSError 实例并在 removeItemAtPath 之后检查它
-
使用 - (BOOL)fileExistsAtPath:(NSString *)path;检查图像是否存在,如果返回YES,则表示您的删除失败
-
刚刚对其进行了测试,它肯定会被删除,并且该删除反映在
contentsOfDirectoryAtPath中(即此处不涉及目录缓存)。所以你一定有一些简单的错误,当你查看NSError的内容时应该会很明显。
标签: ios objective-c xcode nsdocumentdirectory