1
我试图使用the solution here中看到的方法删除我正在编写的应用程序的iPhone文档目录中的所有文件。为了传入文档目录的字符串位置,我对解决方案中的代码做了一些小的修改。我的代码版本如下:iOS - 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的路径看起来是正确的,所以我不知道我是什么做错了。任何人都可以指出我的错误在哪里?非常感谢!
胜利。谢谢你,先生。按照描述工作,我很欣赏关于为什么我以前的方法不起作用的解释。 – golmschenk 2013-04-22 16:28:39
更好的是,调用' - [NSFileManager contentsOfDirectoryAtURL:...]'并避免完全使用路径 – 2013-04-24 22:13:01
使用'path'而不是'absoluteString'解决了我的问题! – Fogh 2014-03-05 07:57:52