ios - 以编程方式从 NSCachesDirectory 中删除文件

标签 ios objective-c cocoa-touch

如果我在我的应用程序中以编程方式下载了 NSCachesDirectory 中的文件,是否也可以以编程方式删除这些文件?

最佳答案

从目录中获取文件的完整路径可能有很多方法,下面是其中的一种。

NSArray *myPathList = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *mainPath    = [myPathList  objectAtIndex:0];

mainPath = [mainPath stringByAppendingPathComponent:DirectoryName];

这里的ma​​inPath是文件的完整路径。

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL fileExists = [fileManager fileExistsAtPath:mainPath];

if (fileExists)
{
     BOOL success = [fileManager removeItemAtPath:mainPath error:&error];
     if (!success) NSLog(@"Error: %@", [error localizedDescription]);

}

关于ios - 以编程方式从 NSCachesDirectory 中删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15064854/

相关文章:

iphone - iOS UICollectionView 放大中心单元格

ios - 赋予 getVaList 的属性未显示在 Crashlytics 日志文件中

objective-c - 阻止导航 Controller 导航并要求用户确认

ios - 如何在 touchesBegan 上调整自定义 UIView 中的 UILabel 位置?

objective-c - Mediaplayer 没有专辑插图占位符

ios - 如何获取 iOS 应用程序的 wordpress 客户端 ID 和客户端密码?

objective-c - 如何使用 block 更改委托(delegate)方法?

objective-c - 有没有快速的方法知道 UITableView 中设备屏幕外的单元格是什么?

ios - Iphone,获取 NSArray 中的国家/地区列表

iphone - UITableView 总是返回 2147483647