2012-03-28 20 views
0

我想了解来自Apple“ComplexBrowser”的示例,但它很难找到任何材料/教程“CFURLEnumeratorCreateDirectoryURL”。可可NSBrowser和CFURLEnumeratorCreateDirectoryURL

ComplexBrowser Sample from Apple

究竟在这段代码是怎么回事?

我不明白这种循环CFURLEnumeratorGetNextURL和东西的方式。

对我来说,NSFileManager的方法似乎更简单,但更有限?

NSArray *contentsAtPath = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:parentPath error:NULL];

- (NSArray *)children { 
if (_children == nil || _childrenDirty) { 
    // This logic keeps the same pointers around, if possible. 
    NSMutableArray *newChildren = [NSMutableArray array]; 

    CFURLEnumeratorRef enumerator = CFURLEnumeratorCreateForDirectoryURL(NULL, (CFURLRef) _url, kCFURLEnumeratorSkipInvisibles, (CFArrayRef) [NSArray array]); 
    NSURL *childURL = nil; 
    CFURLEnumeratorResult enumeratorResult; 
    do { 
     enumeratorResult = CFURLEnumeratorGetNextURL(enumerator, (CFURLRef *) &childURL, NULL); 
     if (enumeratorResult == kCFURLEnumeratorSuccess) { 
      FileSystemNode *node = [[[FileSystemNode alloc] initWithURL:childURL] autorelease]; 
      if (_children != nil) { 
       NSInteger oldIndex = [_children indexOfObject:childURL]; 
       if (oldIndex != NSNotFound) { 
        // Use the same pointer value, if possible 
        node = [_children objectAtIndex:oldIndex]; 
       } 
      } 
      [newChildren addObject:node]; 
     } else if (enumeratorResult == kCFURLEnumeratorError) { 
      // A possible enhancement would be to present error-based items to the user. 
     } 
    } while (enumeratorResult != kCFURLEnumeratorEnd); 

    [_children release]; 
    _childrenDirty = NO; 
    // Now sort them 
    _children = [[newChildren sortedArrayUsingComparator:^(id obj1, id obj2) { 
     NSString *objName = [obj1 displayName]; 
     NSString *obj2Name = [obj2 displayName]; 
     NSComparisonResult result = [objName compare:obj2Name options:NSNumericSearch | NSCaseInsensitiveSearch | NSWidthInsensitiveSearch | NSForcedOrderingSearch range:NSMakeRange(0, [objName length]) locale:[NSLocale currentLocale]]; 
     return result; 
    }] retain]; 
} 

return _children; 

}

+0

欢迎来到Stack Overflow(SO)!请点击问题旁边的箭头接受帮助您的答案。因此告诉别人哪个答案是正确的,并且感谢那些给予他们声望点的帮助你的人! – lnafziger 2012-03-30 20:59:01

回答

1

由于此信息存储在一个不透明的C数据类型,在核心基础,它们提供的C例程,给你关于数据的信息。这是一种封装形式,以便他们可以在幕后改变事物,而不会影响库的公共接口。

基本上,它们创建一个循环并不断询问目录中的下一个URL,直到找到目录的结尾。

  • enumerator是一种索引,可以跟踪它们在URL列表中的位置。
  • enumeratorResult告诉我们我们是否有一个新的URL(或有 是一个错误,或者我们在最后一个记录)。

当它经过的每个URL,它创建FileSystemNode的并将它们添加到一个数组,然后返回,当它完成通过所有的URL在目录中的循环数组。

+0

@RafaelCordoba这个回答你的问题? – lnafziger 2012-04-04 03:19:16

+0

是的,它回答了我的问题,我明白这个普查员是如何工作的。谢谢! – 2012-04-11 05:26:40