objective-c iPhone:NSFilemanager fileExistsAtPath:isDirectory:工作不正常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1196885/
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
iPhone: NSFilemanager fileExistsAtPath:isDirectory: not working properly?
提问by Yllier
I'm working on an app for jailbroken iPhones. I'm trying to get only the directories of an folder. so I'm doing this:
我正在开发一款适用于越狱 iPhone 的应用程序。我试图只获取文件夹的目录。所以我这样做:
NSArray *contentOfFolder = [[NSFileManager defaultManager] directoryContentsAtPath:path];
NSLog(@"contentOfFolder: %@", contentOfFolder);
directoriesOfFolder = [[NSMutableArray alloc] initWithCapacity:100];
for (NSString *aPath in contentOfFolder) {
NSLog(@"apath: %@", aPath);
BOOL isDir;
if ([[NSFileManager defaultManager] fileExistsAtPath:aPath isDirectory:&isDir] &&isDir)
{
[directoriesOfFolder addObject:aPath];
NSLog(@"directoriesOfFolder %@", directoriesOfFolder);
}
}
NSLog(@"dirctories %@", directoriesOfFolder);
but look at what I get. when I get the content of the folder everything looks fine:
但看看我得到了什么。当我获得文件夹的内容时,一切看起来都很好:
2009-07-28 23:23:35.930 Drowser[573:207] new path /private/var 2009-07-28 23:23:35.945 Drowser[573:207] contentOfFolder: ( Keychains, "Managed Preferences", MobileDevice, backups, cache, db, ea, empty, folders, lib, local, lock, log, logs, mobile, msgs, preferences, root, run, spool, stash, tmp, vm )
2009-07-28 23:23:35.930 Drowser[573:207] 新路径 /private/var 2009-07-28 23:23:35.945 Drowser[573:207] contentOfFolder: (Keychains, "Managed Preferences", MobileDevice备份、缓存、db、ea、空、文件夹、lib、本地、锁定、日志、日志、移动、消息、首选项、根、运行、假脱机、存储、tmp、vm)
but then:
但是之后:
2009-07-28 23:23:35.950 Drowser[573:207] apath: Keychains 2009-07-28 23:23:35.954 Drowser[573:207] apath: Managed Preferences 2009-07-28 23:23:35.959 Drowser[573:207] apath: MobileDevice 2009-07-28 23:23:35.984 Drowser[573:207] apath: backups 2009-07-28 23:23:35.993 Drowser[573:207] apath: cache 2009-07-28 23:23:36.002 Drowser[573:207] apath: db 2009-07-28 23:23:36.011 Drowser[573:207] apath: ea 2009-07-28 23:23:36.019 Drowser[573:207] apath: empty 2009-07-28 23:23:36.028 Drowser[573:207] apath: folders 2009-07-28 23:23:36.037 Drowser[573:207] apath: lib 2009-07-28 23:23:36.046 Drowser[573:207] directoriesOfFolder ( lib )
2009-07-28 23:23:35.950 Drowser[573:207] apath:钥匙扣 2009-07-28 23:23:35.954 Drowser[573:207] apath:管理首选项 2009-07-239 D:238 [573:207] apath: MobileDevice 2009-07-28 23:23:35.984 Drowser[573:207] apath: 备份 2009-07-28 23:23:35.993 Drowser[573:207] apath: cache-072 28 23:23:36.002 Drowser [573:207] apath: db 2009-07-28 23:23:36.011 Drowser[573:207] apath: ea 2009-07-28 23:23:36.053 apath:空 2009-07-28 23:23:36.028 Drowser[573:207] apath:文件夹 2009-07-28 23:23:36.037 Drowser[573:207] apath:lib 2009-037-23:28 36.046 Drowser[573:207]目录文件夹(lib)
only "lib"! is recognized as folder. how can that be? the others are folders too. I confirmed it via SSH.
只有“lib”!被识别为文件夹。怎么可能?其他的也是文件夹。我通过SSH确认了它。
does anyone have an idea? Am I doing something wrong?
有没有人有想法?难道我做错了什么?
回答by Dave DeLong
This is a really easy mistake to make, but it's also really easy to fix. Enumerating the contents of a directory only gives you the name of the item, not the item's full path. You have to build the full path yourself. So where you have:
这是一个非常容易犯的错误,但它也很容易修复。枚举目录的内容只会为您提供项目的名称,而不是项目的完整路径。您必须自己构建完整路径。所以你有:
for (NSString *aPath in contentOfFolder) {
NSLog(@"apath: %@", aPath);
BOOL isDir;
if ([[NSFileManager defaultManager] fileExistsAtPath:aPath isDirectory:&isDir] &&isDir) {
[directoriesOfFolder addObject:aPath];
NSLog(@"directoriesOfFolder %@", directoriesOfFolder);
}
}
You should actually have this:
你实际上应该有这个:
for (NSString *aPath in contentOfFolder) {
NSString * fullPath = [path stringByAppendingPathComponent:aPath];
BOOL isDir;
if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDir] &&isDir) {
[directoriesOfFolder addObject: fullPath];
}
}

