我正在尝试使用此处解决方案中看到的方法来删除我正在编写的应用程序的 iPhone 文档目录中的所有文件。为了传递文档目录的字符串位置,我对解决方案中的代码进行了一些小的更改。我的代码版本如下:
NSString *directory = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] absoluteString];
NSLog(@"%@", directory);
NSError *error = nil;
NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directory error:&error];
if (error == nil) {
for (NSString *path in directoryContents) {
NSString *fullPath = [directory stringByAppendingPathComponent:path];
BOOL removeSuccess = [[NSFileManager defaultManager] removeItemAtPath:fullPath error:&error];
if (!removeSuccess) {
// Error handling
}
}
} else {
// Error handling
NSLog(@"%@", error);
}
但是,当我尝试运行它时,由于传递的内容被解释为不存在的目录,因此 directoryContents 的设置失败。具体来说,我在代码中放入的两个 NSLog() 语句返回以下内容:
2013-04-22 11:48:22.628 iphone-ipcamera[389:907] file://localhost/var/mobile/Applications/AB039CDA-412B-435A-90C2-8FBAADFE6B1E/Documents/
2013-04-22 11:48:22.650 iphone-ipcamera[389:907] Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x1d5232c0 {NSUnderlyingError=0x1d54c420 "The operation couldn’t be completed. No such file or directory", NSFilePath=file://localhost/var/mobile/Applications/AB039CDA-412B-435A-90C2-8FBAADFE6B1E/Documents/, NSUserStringVariant=(
Folder
)}
据我所见,打印到 NSLog 的路径看起来是正确的,所以我不确定我做错了什么。谁能指出我的错误在哪里?非常感谢!