Deleting all the files in the iPhone sandbox (documents folder)?

前端 未结 7 2102
清歌不尽
清歌不尽 2020-11-28 05:50

Is there an easy way to delete all the files(images) I saved in the documents folder of the app?

7条回答
  •  渐次进展
    2020-11-28 06:34

    It may not be applicable in all cases but generally it would be more efficient to put all the files in a custom directory inside Documents Directory and then use removeItemAtPath:error: to delete that directory and create it again. E.g.:

    // Clear cache
    NSError *error;
    [[NSFileManager defaultManager] removeItemAtPath:cacheDirectory error:&error];
    if (error)
    {
        // error handling
    }
    
    // Create cache directory again
    NSError *error2;
    [[NSFileManager defaultManager] createDirectoryAtPath:cacheDirectory withIntermediateDirectories:YES attributes:nil error:&error2];
    if (error2)
    {
        // error handling
    }
    

提交回复
热议问题