开发者社区 问答 正文

关于NSCachesDirectory中删除文件 的问题

如果应用实现NSCachesDirectory下载文件,我想知道是不是也能通过编程的方法从 NSCachesDirectory中删除文件?

展开
收起
爵霸 2016-03-24 11:51:57 2537 分享 版权
1 条回答
写回答
取消 提交回答
  • 有很多方法,给你推荐一种:

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

    在这里的mainPath是文件的全路径。

    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]);
    
    }
    2019-07-17 19:12:32
    赞同 展开评论
问答地址: