objective-c 如何删除应用程序指定目录中的所有文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2226600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete ALL FILES in a specified directory on the app?
提问by RexOnRoids
Given a directory [[self documentsDirectory] stringByAppendingPathComponent:@"Photos/"]how do I delete ALL FILES in this folder?
给定一个目录,[[self documentsDirectory] stringByAppendingPathComponent:@"Photos/"]如何删除此文件夹中的所有文件?
(assume a correct documents directory path)
(假设一个正确的文件目录路径)
回答by coneybeare
NSFileManager *fm = [NSFileManager defaultManager];
NSString *directory = [[self documentsDirectory] stringByAppendingPathComponent:@"Photos/"];
NSError *error = nil;
for (NSString *file in [fm contentsOfDirectoryAtPath:directory error:&error]) {
BOOL success = [fm removeItemAtPath:[NSString stringWithFormat:@"%@%@", directory, file] error:&error];
if (!success || error) {
// it failed.
}
}
I leave it up to you to do something useful with the error if it exists.
如果错误存在,我会让你对错误做一些有用的事情。
回答by beryllium
if you want to remove files and the directory itself then use it without forloop
如果你想删除文件和目录本身,然后使用它而不for循环
NSFileManager *fm = [NSFileManager defaultManager];
NSString *directory = [[self documentsDirectory] stringByAppendingPathComponent:@"Photos"];
NSError *error = nil;
BOOL success = [fm removeItemAtPath:cacheImageDirectory error:&error];
if (!success || error) {
// something went wrong
}
回答by Prcela
same for swift lovers:
对于快速爱好者来说也是如此:
let fm = FileManager.default
do {
let folderPath = "...my/folder/path"
let paths = try fm.contentsOfDirectory(atPath: folderPath)
for path in paths
{
try fm.removeItem(atPath: "\(folderPath)/\(path)")
}
} catch {
print(error.localizedDescription)
}
回答by strikerdude10
Most of the older answers have you use contentsOfDirectoryAtPath:error:which will work, but according to Apple:
您使用的大多数较旧的答案都可以使用contentsOfDirectoryAtPath:error:,但根据 Apple 的说法:
"The preferred way to specify the location of a file or directory is to use the NSURL class"
“指定文件或目录位置的首选方法是使用 NSURL 类”
so if you want to use NSURL instead you can use the method contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:so it would look something like this:
所以如果你想改用 NSURL,你可以使用这个方法,contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:所以它看起来像这样:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray<NSURL*> *urls = [fileManager contentsOfDirectoryAtURL:directoryURL includingPropertiesForKeys:@[NSURLNameKey, NSURLIsDirectoryKey] options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
for (NSURL *url in urls)
{
NSError *error = nil;
BOOL success = [fileManager removeItemAtURL:url error:error];
if (!success || error) {
// something went wrong
}
}
回答by dimo hamdy
Swift 4
斯威夫特 4
do {
let destinationLocation:URL = ...
if FileManager.default.fileExists(atPath: destinationLocation.path) {
try! FileManager.default.removeItem(at: destinationLocation)
}
} catch {
print("Error \(error.localizedDescription)")
}

