iPhone: NSFilemanager fileExistsAtPath:isDirectory: not working properly?

流过昼夜 提交于 2019-12-03 07:20:29

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];
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!