sre*_*vas 3 iphone nsfilemanager ios4
我在使用设备时使用代码删除临时目录文件.
-(void) clearAllTempFiles {
NSString *path = NSTemporaryDirectory();
if ([path length] > 0)
{
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL deleted = [fileManager removeItemAtPath:path error:&error];
if (deleted != YES || error != nil)
{
}
else{
// Recreate the Documents directory
[fileManager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:&error];
}
}
}
Run Code Online (Sandbox Code Playgroud)
它不能正常工作?这是从临时目录中删除文件的正确代码?
请帮助我?
小智 6
您可以在代码中使用此命令获取mac上的tmp目录名称:
码:
(void)cacheDirectory {
NSString *tempPath = NSTemporaryDirectory();
NSLog(@"Temp Value = %@", items);
}
Run Code Online (Sandbox Code Playgroud)
从任何地方调用方法.
这将返回tmp文件夹名称,然后在finder do(cmd-shift-G)中粘贴您从控制台窗口获得的响应.
以下将清除Simulator使用的TMP目录.
码:
NSString *tempPath = NSTemporaryDirectory();
NSArray *dirContents = [[NSFileManager defaultManager] directoryContentsAtPath:tempPath];
NSArray *onlyJPGs = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.jpg'"]];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (onlyJPGs) {
for (int i = 0; i < [onlyJPGs count]; i++) {
NSLog(@"Directory Count: %i", [onlyJPGs count]);
NSString *contentsOnly = [NSString stringWithFormat:@"%@%@", tempPath, [onlyJPGs objectAtIndex:i]];
[fileManager removeItemAtPath:contentsOnly error:nil];
}
Run Code Online (Sandbox Code Playgroud)
上面的代码只从目录中清除JPG,所以如果你想清除其他任何东西,那么修改它.
我找到了一个简单的
NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
for (NSString *file in tmpDirectory) {
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
}
Run Code Online (Sandbox Code Playgroud)