iPhone:NSFilemanager fileExistsAtPath:isDirectory:无法正常工作? [英] iPhone: NSFilemanager fileExistsAtPath:isDirectory: not working properly?

查看:132
本文介绍了iPhone:NSFilemanager fileExistsAtPath:isDirectory:无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款适用于越狱iPhone的应用程序。我正在尝试只获取文件夹的目录。所以我这样做:

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:

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,
托管偏好,
MobileDevice,
备份,
缓存,
db,
ea,
空,
文件夹,
lib,
本地,
锁,
log,
logs,
mobile,
msgs,
preferences,
root,
run,
spool,
stash ,
tmp,
vm

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.950 Drowser [573:207] apath:Keychains
2009-07-28 23:23:35.954 Drowser [573:207] apath:管理偏好
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 :空
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: 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 )

仅lib!被识别为文件夹。怎么可能?其他人也是文件夹。我通过SSH确认了它。

only "lib"! is recognized as folder. how can that be? the others are folders too. I confirmed it via SSH.

有没有人有想法?我做错了什么?

does anyone have an idea? Am I doing something wrong?

推荐答案

这是一个非常容易犯的错误,但它也很容易修复。枚举目录的内容只会为您提供项目的名称,而不是项目的完整路径。你必须自己构建完整的路径。那么你在哪里:

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);
  }
}

你应该实际拥有:

for (NSString *aPath in contentOfFolder) {
  NSString * fullPath = [path stringByAppendingPathComponent:aPath];

  BOOL isDir;
  if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDir] &&isDir) {
    [directoriesOfFolder addObject: fullPath];
  }
}

这篇关于iPhone:NSFilemanager fileExistsAtPath:isDirectory:无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆